I have had a couple of students come upon an error in the code that was provided in the program. At the bottom of the program, inside the “Scoreboard” section, students are receiving an error on the line where the health variable value is coded to be drawn on the screen (text (health, 350, 30);). There’s a red box with an error message at the bottom that says, "ERROR: Line: 62: TypeError: Cannot read properties of undefined (reading ‘toString’). I’m not sure what this error means or how to fix it. Thanks!
If there’s any way you can have one of those students click on the share link and send that link to us, I’d love to look at it. I’m guessing it’s some kind of typo, but without a link, It’s hard to say.
If you aren’t able to send a link, check and make sure that the health variable has been assigned (in the “create the variables” section). It would be a line like this:
var health = 100
The error usually means that you are referring to a variable that hasn’t been declared somewhere else in the code.
thanks,
Mike
Mike
The variable was already created; it was already provided by code.org for that Lesson. It’s the side scroller mini-project, which comes with some code already provided. My computer science class is over for today, so tomorrow I will have a student share their link and send it to you.
Tamra
Greetings @thiggins,
First let’s take a look at the error message and break it up
TypeError: as you know there are specific types of data even though it’s more abstract in JS such are Number, String, Boolean, Object, Array… and I’m sure I’m probably missing a few but you get what I mean
Cannot Read Property: this can usually mean 1 of 2 things
- 1: the variable has been incorrectly declared
- 2: the defined variable is out of scope
Reading .toString: this is a bit more vague out of all the errors but this is a prototype of other constructor to convert the selected data type to a readable string, but since it’s undefined it doesn’t have the .toString prototype property which is most likely what text calls for it to be a readable number
Let’s try to create a probable solution
when creating a variable especially one to be accessed from multiple points it is best to ensure that it remains in the global scope for example
var health = 100;
function hit(amount) {
health -= amount;
}
function display() {
text("Health: " + health, 350, 30)
}
if this doesn’t help you’ll most likely have to wait until tomorrow until either @mwood, me or anyone else will be able to help. Best of luck
Mike
Oddly enough, the error is not to be found today. I will check with another student who received that same error in my next class, too. If it’s not there for them either, then I will guess it was just a weird glitch. We literally made no changes to the provided code, and the error was showing. Now it’s gone. Such is technology!
Tamra
Interesting. Ok. Let us know if it comes back!
Mike
I am having this same issue with a student with the same code. Everything has been put in the exact way the side scroller game asks:
How do I send a link to you to take a look at?
Thanks,
Karen Abraham
Look for the “Share Button” on the top left corner of the page. Click on it and then click on the “copy link” button (or the URL if a URL shows up). That will copy the link and then you can paste it into a reply to this message.
Good luck!
Mike