# Number Guesser Challenge - what's wrong with my code

I (think) I’ve finished the code for this challenge but I’m not sure if it’s supposed to work in the game that’s up, next to the space to code (I coded on Codecademy). Neither am I sure how to test my code if not in that way. I’d be very grateful for any help.

Also, below is my code. Any feedback would be greatly appreciated!!

let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;

const generateTarget = () => {
console.log(Math.floor(Math.random() * 10));
}
generateTarget()

const compareGuesses = (human, computer, secret) => {
let humanGuess = Math.abs(human - secret);
let computerGuess = Math.abs(computer - secret);
if (humanGuess === computerGuess) {
return true;
} else if (humanGuess > computerGuess) {
return false;
} else {
return true;
}
};
compareGuesses();

const updateScore = () => {
if (human) {
return humanScore++;
} else {
return computerScore++;
}
}

const advanceRound = () => {
if (human || computer) {
return currentRoundNumber++
}
}

Further, in the game next to the scipt.js, the Target Number is returning as undefined, but on the code checker in the script.js it’s returning a random whole number.
Moreover, when I console.log the computerScore and the humanScore they return as 0.
currentRoundNumber isn’t incrementing either.

Hi there.

I haven’t fully analyzed your code yet (cause it’s kinda hard to read it if it’s not formatted properly), but I think I found an error.

Step 5 of the challenge says:

Create an `updateScore()` function. This function will be used to correctly increase the winner’s score after each round.

This function:

• Has a single parameter. This parameter will be a string value representing the winner.
• Increases the score variable ( `humanScore` or `computerScore` ) by 1 depending on the winner passed in to `updateScore` . The string passed in will be either `'human'` or `'computer'` .
• Does not need to return any value.

There it specifies your `updateScore()` should take one parameter: a string representing the winner (either `'human'` or `'computer'`.
Your `updateScore()` function, however, is not taking any parameters at all…

There’s also another error within that function.

So…you’re using `human` as the condition for your `if`. But, what’s `human`? The way you wrote it, it’s like you have a variable called `human` somewhere in your program, and you’re checking whether its value is truthy or falsy.
But you certainly do not have a `human` variable anywhere in your program.

What you should be doing is checking whether the parameter your `updateScore()` function takes, has the value `'human'` or `'computer'` (both strings).

Just as stated in Step 5:

1 Like

Thanks so much Yizuhi. I’ve added the code below once more, this time properly formatted, well, to the best of my ability, I hope it is proper! I’ve also added the parameters ‘human’ and ‘computer’ to updateScore(). It’s still returning 0. Further, I’ve added ‘human’, ‘computer’ to advanceRound() but it is remaining at 1. Is it because I’m not calling the functions???

let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;

const generateTarget = () => {
console.log(Math.floor(Math.random() * 10));
}
generateTarget()

const compareGuesses = (human, computer, secret) => {

let humanGuess = Math.abs(human - secret);
let computerGuess = Math.abs(computer - secret);

``````if (humanGuess === computerGuess) {
return true;
``````

} else if (humanGuess > computerGuess) {
return false;

} else if (humanGuess > 9) {
}

``````else {
return true;
``````

}
};
compareGuesses();

const updateScore = (human, computer) => {

``````if (human) {
return humanScore++;
``````

}
else {
return computerScore++;
}
}
console.log(humanScore);
console.log(computerScore);

const advanceRound = (human, computer) => {
if (human || computer) {
return currentRoundNumber++
}
}

console.log(currentRoundNumber);

No problem, I’m happy to help

It is better, but I think you should check out this thread

Hmm…that’s not really the way to do it. A parameter in a function is like a variable, and you give some value to that variable when you call the function.
Something like this:

``````// Define your function, which takes a parameter called "aParameter"
const logParameterValue = (aParameter) => {
console.log("This is the value that was given to the parameter: " + aParameter)
}

// Call your function and give the parameter a value of 'I love cats!' (a string)
myFunction("I love cats!")

// This will be logged to the console:
// This is the value that was given to the parameter: I love cats!
``````

Notice how the instructions for Step 5 specified that the `updateScore()` function should take one parameter, and yours is taking two parameters (`human` and `computer`). Take only one parameter. That parameter could take the value of `'human'` or `'computer'` (both are strings). Then, in your `if`, you can check if the value of your parameter equals `'human'` or `'computer'`.

I checked the instructions, and, if I’m not wrong, Step 6 says that the `advanceRound()` function should simply increase the value of `currentRoundNumber` by 1. Nothing more, nothing less. You don’t really need this function to take any parameters, and the `if` is not necessary either.

No, don’t worry about that. Step 2 says:

In this project, you’ll write four functions in script.js . We’ve provided some additional JavaScript code in game.js that will call your functions based on user interactions, but you don’t need to look at game.js and shouldn’t edit it if you want your project to work as intended. As you complete this project, make sure that all of your functions are named exactly as specified so that they can be called correctly when the game is played.

Your functions are being called, but in another file called game.js.

1 Like

Thank you! I’ve added this to my favourites!

Fantastic! Thank you! This is working now!

This is also working now!

As you can see above, I was using the `++` after `humanScore`. I’ve placed them as follows now: `++humanScore`.

However, I’m still left with one problem and a possible other one. In the game, everything is working perfectly now but the Target Number remains Undefined and the user is always beating the computer! The random target number is showing up on the console though.

Yay! So glad those things are working now

Looking at your code, it makes sense. Your `generateTarget()` function is only logging to the console a random number.
Here is what Step 3 says:

Create a `generateTarget()` function. This function will be called at the start of each new round in order to generate the new secret target number.

This function should return a random integer between `0` and `9` .

Return a random integer. That’s what your function should do. Not logging it.

Hope this helps you

Thank you! I’ve done this and it’s working now!

There’s only one thing left which isn’t working. The game is only updating the human score and even when the computer wins the human score increments by 1. I’ve tried changing the parameter to `computer` and `winner` but that just means that the neither score updates and for some reason I can’t press New Round. Please see my code below. Honestly, forever grateful for your help!

const `updateScore` = (human) => {
if (‘human’) {
return `++humanScore`;
} else if (‘computer’) {
return ++`computerScore`;
}
}

Currently, this code checks to see if the string `"human"` is a truthy value. A truthy value is anything that isn’t and empty array, empty string, `false`, `null` `NaN`, `undefined`, etc. Here is an article on them. The string `"human"`, however is a truthy value. This means the the `return ++human` code will always run, and, since the `++computerScore` is in an `else if` block, it will never run.

Consider comparing the human and computer guess to see which one is closer, and incrementing the score accordingly. You can use the `computerGuess` function to do this.

I have edited the code to the following. Note that I included `compareGuesses`. Now it’s only incrementing the Computer Score regardless of who wins!

const `updateScore` = (human) => {

``````if (human && `compareGuesses` === true) {
return ++humanScore;
``````

} else {
return ++computerScore;
}
}

Remember what I told you about parameters?

They’re like variables. When the function is called, a specific value will be given to them. In this case, your `updateScore()` function is being called by another file (game.js). It will give the parameter the corresponding value depending on who wins:

If the human wins, the parameter’s value will be the string `‘human’`.
If the computer wins, the parameter’s value will be the string `‘computer’`.

Try changing the name of the parameter your function takes to `winner`. This won’t change anything in the functionality, but I think it will be useful to remind you what that parameter really is: a String that will be either `‘human’` or `‘computer’`.

Then, in your `if`, it’s time to check what value was given to the `winner` parameter of `updateScore()`. If `winner == ‘human’`, then the user won. Increment his score. Otherwise, the computer won. Increment its score.
You should be checking what value was assigned to the `winner` parameter when the function was called (you could go to the file game.js to verify it’s being called, just don’t change anything in there).

1 Like

Thank you! That’s a lot clearer for me now. I changed the parameter to `winner` and the the `if` statement to `if` `winner == 'human'` and now its working! I’m not sure why, however, sometimes the parameter can be left blank and work and why that wasn’t the case with `updateScore()`.

Yay! So glad it’s working now!

With “leaving the parameter blank”, do you mean that the function doesn’t take any parameters? Some functions just don’t need to be provided with outer data to do their job.
For example, the `generateTarget()` function. It just outputs a random number between 0 and 9, and that’s it. It doesn’t need to know anything about what’s happening in the rest of your program.

The `updateScore()` function, however…how does it know which score to increase? You must tell the function (through the `winner` parameter) whose score should be increased.

1 Like

Yes, I meant “take any parameters”. I see now! Thanks again!

1 Like