Ask coding questions

← Back to all posts
TypeError: Cannot read property '0' of undefined
adsarebbbad (157)

the error is at line 27. i don't know why its undefined, because i just declared it at line 26

Answered by Coder100 (8721) [earned 5 cycles]
View Answer
Commentshotnewtop
Coder100 (8721)

Well, you see, available.length when you splice it on line 30 returns undefined because available.length is 0.

adsarebbbad (157)

huh. i thought i added some stuff to available @Coder100

adsarebbbad (157)

available.push on line 19 but it still comes undefined @Coder100

Coder100 (8721)

make sure that function is called first @adsarebbbad

Coder100 (8721)

also you never called setup lol @adsarebbbad

adsarebbbad (157)

but how come it isn't being ran first? @Coder100

Coder100 (8721)

ohh I see, you did a splice so the original array got affected (available) continously @adsarebbbad

adsarebbbad (157)

but i don't know how to fix it without it affecting the game @Coder100

Coder100 (8721)

@adsarebbbad Call nextTurn(); in a mouseClicked function maybe?

It's not like nextTurn() happens everytime

adsarebbbad (157)

not the expected result but it works @Coder100

Coder100 (8721)

i mean it looks like its working to me (speaking from the computer's perspective) @adsarebbbad

adsarebbbad (157)

just not the expected result @Coder100

SixBeeps (3064)

Not sure why, but the code works fine if you open it in a new tab.