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 :(
javascript html
add a comment |
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 :(
javascript html
2
Should beString.charAt(0)
instead ofString.charAt[0]
– rv7
Nov 7 at 18:22
add a comment |
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 :(
javascript html
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
javascript html
asked Nov 7 at 18:21
Jakub Lipiński
31
31
2
Should beString.charAt(0)
instead ofString.charAt[0]
– rv7
Nov 7 at 18:22
add a comment |
2
Should beString.charAt(0)
instead ofString.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
add a comment |
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().
My god im so thankful... It works now
– Jakub Lipiński
Nov 7 at 18:25
add a comment |
up vote
0
down vote
You could simply use odpowiedzi[0][0] - no need to use charAt() :)
add a comment |
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().
My god im so thankful... It works now
– Jakub Lipiński
Nov 7 at 18:25
add a comment |
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().
My god im so thankful... It works now
– Jakub Lipiński
Nov 7 at 18:25
add a comment |
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().
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));
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
add a comment |
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
add a comment |
up vote
0
down vote
You could simply use odpowiedzi[0][0] - no need to use charAt() :)
add a comment |
up vote
0
down vote
You could simply use odpowiedzi[0][0] - no need to use charAt() :)
add a comment |
up vote
0
down vote
up vote
0
down vote
You could simply use odpowiedzi[0][0] - no need to use charAt() :)
You could simply use odpowiedzi[0][0] - no need to use charAt() :)
answered Nov 7 at 18:49
schlock
136129
136129
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
Should be
String.charAt(0)
instead ofString.charAt[0]
– rv7
Nov 7 at 18:22