Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Why won't my score function work?

This is going to be a lot of code to look at, so if you take the time I really appreciate it. I have made an n-back matching game that would be fully functional, except that the score function just won't work. It is supposed to do the calculation based on the number of matches that take place and the number of matches the player gets. All the numbers are valid numbers, yet the result is NaN. Because the code is too much to paste here, I am pasting a Dropbox link to all the files.

The main issue either has to do with the get score() method, the update_score() method, or the update_nback() method. If there is something else in the code that isn't working then I have no idea what it is.

I am very frustrated over this and just cannot figure it out. At one point I did have it working correctly and I have no idea what I did. I guess I really need to learn Git.

https://www.dropbox.com/sh/e667ecvxk7gn0bt/AABDp2o77cMagdhqCnY2QzJOa?dl=0

This will be a great challenge for someone who has completed the Object-Oriented JavaScript course or is in the JavaScript techdegree and wants to test their troubleshooting skills. I am at the point of giving up and need someone with a bit more experience.

1 Answer

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,716 Points

In initialize_game, on lines 99-100, you set these properties as 0 integer values:

this.matches = 0;
this.player_matches = 0;

Then in the score getter, on line 60, you're trying to find the length property of an integer:

this.player_matches.length / this.matches.length

The length property of an integer doesn't exist, so is undefined. And undefined / undefined is NaN.

One suggestion: doing a whole string of operations in one line like this isn't necessarily a good idea:

get score() { return (Math.round((this.player_matches.length / this.matches.length) * 100) - (this.error_count * 3));

It makes it hard to read and debug. Better to do each step one at a time and store that in a variable. Then you can print out each variable at each step of the process of need be to zero in on the problem.

Also, one tool I've learned to love is TypeScript. There's a learning curve, and there some overhead getting your project set up to transpile from typescript to js. But the benefit is that it catches the kinds of issues that happened here, where you're trying to access a property on a type that doesn't have that property. The TypeScript compiler would show you errors in your IDE and output in the terminal, not silently allowing bugs to happen.

And yes, please learn Git.

Hi Brendan, thanks for your response. The initialize_game() method actually works correctly. At the beginning of each game session a number of property values need to be reset in order for the game to operate correctly. For instance, game.error_count and game.matches must be reset or else each subsequent game session will be calculating the player score based on previous sessions. The same logic applies to other properties that are reset at each game session.

As for the this.player_matches.length / this.matches.length computation that was a typo, thank you for pointing it out.

I was ultimately able to solve this problem. The game.score pseudo-property returned NaN for an unknown reason, but began returning a number once I changed the matches and player matches properties to be arrays rather than integers.

Happy coding!

Hey Brendan. Yes, you did give an explanation, but your explanation was wrong. I changed the code after posting it and still had the same issue. After I changed the code, the computation was being done on valid numbers in JavaScript and the properties containing those numbers were indeed defined. Therefore the reason why the score getter still returned NaN is unknown and now that I have the program operating correctly I probably won't worry about it. Again, thank you for your very kind advice!