The console.log() statements should help debugging the function as they provide you with a way to “look into it” while it is running (outputting variables, letting you known when a certain branch is run etc.). However, i’ve noticed that i missed parentheses around the secretNumber - user/computerGuess calculation. Missing those, the NaN (Not a Number) returned in the log might have confused more than helped.
Let’s do it by example, starting with your original function:
//Should return true if humanGuess is closer or equal as close to the target as computerGuess
const compareGuesses = (userGuess, computerGuess, secretNumber) => {
console.log("userGuess: " + userGuess + ", computerGuess: " + computerGuess + ", secretNumber: " + secretNumber);
console.log("LHS: " + (secretNumber - userGuess));
console.log("RHS: " + (secretNumber - computerGuess));
if (secretNumber - userGuess >= secretNumber - computerGuess) {
return true
} else {
return false
}
}
//Tests for compareGuesses
//Check if the function works when the guess closer to the target is bigger than the target
console.log(compareGuesses(6,3,5)); //should return true
console.log(compareGuesses(3,6,5)); //should return false
console.log(compareGuesses(6,4,5)); //should return true
//Check if the function works when the guess closer to the target is smaller than the target
console.log(compareGuesses(4,7,5)); //should return true
console.log(compareGuesses(7,4,5)); //should return false
console.log(compareGuesses(4,6,5)); //should return true
Looking at the log, two things can be seen:
- The function only works when the guess closer to the target is smaller than the target.
- If we find a way to ignore the signs, we could just check if LHS <= RHS and return true if it is.
Here comes Math.abs() into play. Math.abs(5-4)
and Math.abs(5-6)
both return 1. So rewriting the function like that solves the issue:
//Should return true if humanGuess is closer or equal as close to the target as computerGuess
const compareGuesses = (userGuess, computerGuess, secretNumber) => {
if (Math.abs(secretNumber - userGuess) <= Math.abs(secretNumber - computerGuess)) {
return true
} else {
return false
}
}
//Tests for compareGuesses
//Check if the function works when the guess closer to the target is bigger than the target
console.log(compareGuesses(6,3,5)); //should return true
console.log(compareGuesses(3,6,5)); //should return false
console.log(compareGuesses(6,4,5)); //should return true
//Check if the function works when the guess closer to the target is smaller than the target
console.log(compareGuesses(4,7,5)); //should return true
console.log(compareGuesses(7,4,5)); //should return false
console.log(compareGuesses(4,6,5)); //should return true
Regarding Math.random() the MDN Page states “pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1)”. So the biggest number given by Math.random() is 0.9999999… This number times 9 gives 8.9999999999… Flooring it gives 8. So the biggest number returned by Math.floor(Math.random()*9)
is 8. You need to multiply with 10 to get a range from 0 to 9.
Your updateScore function looks correct now. Making the changes in the compareGuess function and in generateTarget will solve all issues.