I have a student working on his own app (not for AP) that could use some help debugging the ‘undefined’ error code.
**Link to the project or level:**Code.org - App Lab
**What I expect to happen:**He is trying to push one of the player buttons to add a rebound for the player. The list is where the rebound count should start.
What actually happens: It doesn’t count the rebounds using the index. Errors and says ‘players is undefined’.
What I’ve tried: He tried to remove the purple players from the events and changed up the ‘addRebounds’ function.
I mean, I don’t see why it would be working. Your student is settings an array to a number, not an array, so the next time you press a button, the program tries to run addRebounds()
, it just fails to do it, as players
became an integer the last time addRebounds
was run. If it’s hard to understand, here’s a version with what exactly the program does when a button is pressed. Oh, also another problem is that your student sets players to a number himself on multiple lines.
addRebounds
is run.
Console:
Ok, run addRebounds.
The player variable turns into a number 1 time (12) after that, it turns into NaN 4 times, as you can’t get the i element of a number.
I hope that explains why it isn’t working, however I’ll now show how to fix this
First of all, remove these:
Second of all, in the addRebounds function, don’t set the whole array (players) to a number, set one element of it only.(in other words, turn this to this )
That seems to be it to set the players array to the right thing, however you can’t set an App Lab element’s text to an array. That’s why when you will try to run the code, the text won’t change, it will put out this warning instead:
To fix this, I just used the .join javascript array method and it gave me what I wanted.
Here’s the full code if you need it:
Thank you so much for the feedback. I am very new to this and struggle to keep up with it all. Your link to the new code just takes me to my dashboard. Any chance you can re-send it? Thank you so much again!
Oh god… I messed up a bit, I don’t know how…
Anyway, here’s the link to the code: Code.org - App Lab
I understood that I had to do that, but after a few minutes of thinking, I realized your student probably wants only one player’s score to go up. In that case he has to assign an event to every single one of the buttons.