Array strings come out as undefined











up vote
-2
down vote

favorite












So i've got an array of 9 strings, predefined, and when i try to call them out in a function they come out as undefined, i don't really know what am i doing wrong :/



function checkAns(){
var nr_pytania = (document.getElementById("q_id").value)-1;
var odpowiedz = document.getElementById("answer").value;
var odpowiedzi = ["Mazury", "Korfanty", "Paderewski", "Wersalski", "Zaolzie", "Orlęta", "Wisła", "Haller", "Gdańsk"];

console.log(odpowiedzi[0].charAt[0]);
if(odpowiedz == odpowiedzi[nr_pytania])
{
document.getElementById("answer").value = "POPRAWNE!";
for(var i=0; i<odpowiedzi[nr_pytania].length; i++)
{
document.getElementById("letter"+nr_pytania+i).innerHTML = odpowiedzi[nr_pytania].charAt[i];
}
}
else
{
document.getElementById("answer").value = "Odpowiedź błędna!";
}


}



So, the part that is the most important to me is:



    var odpowiedzi = ["Mazury", "Korfanty", "Paderewski", "Wersalski", "Zaolzie", "Orlęta", "Wisła", "Haller", "Gdańsk"];

console.log(odpowiedzi[0].charAt[0]);


Because the console returns undefined, and im not really sure why :(










share|improve this question


















  • 2




    Should be String.charAt(0) instead of String.charAt[0]
    – rv7
    Nov 7 at 18:22

















up vote
-2
down vote

favorite












So i've got an array of 9 strings, predefined, and when i try to call them out in a function they come out as undefined, i don't really know what am i doing wrong :/



function checkAns(){
var nr_pytania = (document.getElementById("q_id").value)-1;
var odpowiedz = document.getElementById("answer").value;
var odpowiedzi = ["Mazury", "Korfanty", "Paderewski", "Wersalski", "Zaolzie", "Orlęta", "Wisła", "Haller", "Gdańsk"];

console.log(odpowiedzi[0].charAt[0]);
if(odpowiedz == odpowiedzi[nr_pytania])
{
document.getElementById("answer").value = "POPRAWNE!";
for(var i=0; i<odpowiedzi[nr_pytania].length; i++)
{
document.getElementById("letter"+nr_pytania+i).innerHTML = odpowiedzi[nr_pytania].charAt[i];
}
}
else
{
document.getElementById("answer").value = "Odpowiedź błędna!";
}


}



So, the part that is the most important to me is:



    var odpowiedzi = ["Mazury", "Korfanty", "Paderewski", "Wersalski", "Zaolzie", "Orlęta", "Wisła", "Haller", "Gdańsk"];

console.log(odpowiedzi[0].charAt[0]);


Because the console returns undefined, and im not really sure why :(










share|improve this question


















  • 2




    Should be String.charAt(0) instead of String.charAt[0]
    – rv7
    Nov 7 at 18:22















up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











So i've got an array of 9 strings, predefined, and when i try to call them out in a function they come out as undefined, i don't really know what am i doing wrong :/



function checkAns(){
var nr_pytania = (document.getElementById("q_id").value)-1;
var odpowiedz = document.getElementById("answer").value;
var odpowiedzi = ["Mazury", "Korfanty", "Paderewski", "Wersalski", "Zaolzie", "Orlęta", "Wisła", "Haller", "Gdańsk"];

console.log(odpowiedzi[0].charAt[0]);
if(odpowiedz == odpowiedzi[nr_pytania])
{
document.getElementById("answer").value = "POPRAWNE!";
for(var i=0; i<odpowiedzi[nr_pytania].length; i++)
{
document.getElementById("letter"+nr_pytania+i).innerHTML = odpowiedzi[nr_pytania].charAt[i];
}
}
else
{
document.getElementById("answer").value = "Odpowiedź błędna!";
}


}



So, the part that is the most important to me is:



    var odpowiedzi = ["Mazury", "Korfanty", "Paderewski", "Wersalski", "Zaolzie", "Orlęta", "Wisła", "Haller", "Gdańsk"];

console.log(odpowiedzi[0].charAt[0]);


Because the console returns undefined, and im not really sure why :(










share|improve this question













So i've got an array of 9 strings, predefined, and when i try to call them out in a function they come out as undefined, i don't really know what am i doing wrong :/



function checkAns(){
var nr_pytania = (document.getElementById("q_id").value)-1;
var odpowiedz = document.getElementById("answer").value;
var odpowiedzi = ["Mazury", "Korfanty", "Paderewski", "Wersalski", "Zaolzie", "Orlęta", "Wisła", "Haller", "Gdańsk"];

console.log(odpowiedzi[0].charAt[0]);
if(odpowiedz == odpowiedzi[nr_pytania])
{
document.getElementById("answer").value = "POPRAWNE!";
for(var i=0; i<odpowiedzi[nr_pytania].length; i++)
{
document.getElementById("letter"+nr_pytania+i).innerHTML = odpowiedzi[nr_pytania].charAt[i];
}
}
else
{
document.getElementById("answer").value = "Odpowiedź błędna!";
}


}



So, the part that is the most important to me is:



    var odpowiedzi = ["Mazury", "Korfanty", "Paderewski", "Wersalski", "Zaolzie", "Orlęta", "Wisła", "Haller", "Gdańsk"];

console.log(odpowiedzi[0].charAt[0]);


Because the console returns undefined, and im not really sure why :(







javascript html






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 18:21









Jakub Lipiński

31




31








  • 2




    Should be String.charAt(0) instead of String.charAt[0]
    – rv7
    Nov 7 at 18:22
















  • 2




    Should be String.charAt(0) instead of String.charAt[0]
    – rv7
    Nov 7 at 18:22










2




2




Should be String.charAt(0) instead of String.charAt[0]
– rv7
Nov 7 at 18:22






Should be String.charAt(0) instead of String.charAt[0]
– rv7
Nov 7 at 18:22














2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted













var odpowiedzi = ["Mazury", "Korfanty", "Paderewski", "Wersalski", "Zaolzie", "Orlęta", "Wisła", "Haller", "Gdańsk"];

console.log(odpowiedzi[0].charAt(0));





You need to change it from charAt[0] to charAt(0).



Additional reading for String.prototype.charAt().






share|improve this answer























  • My god im so thankful... It works now
    – Jakub Lipiński
    Nov 7 at 18:25




















up vote
0
down vote













You could simply use odpowiedzi[0][0] - no need to use charAt() :)






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195497%2farray-strings-come-out-as-undefined%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted













    var odpowiedzi = ["Mazury", "Korfanty", "Paderewski", "Wersalski", "Zaolzie", "Orlęta", "Wisła", "Haller", "Gdańsk"];

    console.log(odpowiedzi[0].charAt(0));





    You need to change it from charAt[0] to charAt(0).



    Additional reading for String.prototype.charAt().






    share|improve this answer























    • My god im so thankful... It works now
      – Jakub Lipiński
      Nov 7 at 18:25

















    up vote
    2
    down vote



    accepted













    var odpowiedzi = ["Mazury", "Korfanty", "Paderewski", "Wersalski", "Zaolzie", "Orlęta", "Wisła", "Haller", "Gdańsk"];

    console.log(odpowiedzi[0].charAt(0));





    You need to change it from charAt[0] to charAt(0).



    Additional reading for String.prototype.charAt().






    share|improve this answer























    • My god im so thankful... It works now
      – Jakub Lipiński
      Nov 7 at 18:25















    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted









    var odpowiedzi = ["Mazury", "Korfanty", "Paderewski", "Wersalski", "Zaolzie", "Orlęta", "Wisła", "Haller", "Gdańsk"];

    console.log(odpowiedzi[0].charAt(0));





    You need to change it from charAt[0] to charAt(0).



    Additional reading for String.prototype.charAt().






    share|improve this answer

















    var odpowiedzi = ["Mazury", "Korfanty", "Paderewski", "Wersalski", "Zaolzie", "Orlęta", "Wisła", "Haller", "Gdańsk"];

    console.log(odpowiedzi[0].charAt(0));





    You need to change it from charAt[0] to charAt(0).



    Additional reading for String.prototype.charAt().






    var odpowiedzi = ["Mazury", "Korfanty", "Paderewski", "Wersalski", "Zaolzie", "Orlęta", "Wisła", "Haller", "Gdańsk"];

    console.log(odpowiedzi[0].charAt(0));





    var odpowiedzi = ["Mazury", "Korfanty", "Paderewski", "Wersalski", "Zaolzie", "Orlęta", "Wisła", "Haller", "Gdańsk"];

    console.log(odpowiedzi[0].charAt(0));






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 7 at 18:26

























    answered Nov 7 at 18:24









    James Ives

    1,2051231




    1,2051231












    • My god im so thankful... It works now
      – Jakub Lipiński
      Nov 7 at 18:25




















    • My god im so thankful... It works now
      – Jakub Lipiński
      Nov 7 at 18:25


















    My god im so thankful... It works now
    – Jakub Lipiński
    Nov 7 at 18:25






    My god im so thankful... It works now
    – Jakub Lipiński
    Nov 7 at 18:25














    up vote
    0
    down vote













    You could simply use odpowiedzi[0][0] - no need to use charAt() :)






    share|improve this answer

























      up vote
      0
      down vote













      You could simply use odpowiedzi[0][0] - no need to use charAt() :)






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You could simply use odpowiedzi[0][0] - no need to use charAt() :)






        share|improve this answer












        You could simply use odpowiedzi[0][0] - no need to use charAt() :)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 7 at 18:49









        schlock

        136129




        136129






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195497%2farray-strings-come-out-as-undefined%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()