Hello, I’ve recently begun learning JavaScript. I try to understand the theory and mentality of everything before moving over. I realize I could just play around and figure out the correct code to make this project function, however then I’d be at a loss. Therefore I humbly ask that someone who knows JavaScript and is familiar with the number guesser game review my code and tell me where I have gone wrong;
let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;
// Write your code below:
// Here I am writing a function that will pick a number from 0 to 9.
function generateTarget() {
return Math.floor(Math.random() * 10)
}
/* So this is basically the main function of the project.
In this function we require 3 numbers: humanGuess, computerGuess and targetGuess.
The program says that it automatically runs generateTarget function at the beginning of each round to decide on the secret number itself. So I'll not be identifying it myself. I do however need the computer to make a guess so I run the same function for the computerGuess variable and then assign it to the variable.
Basically the winner is the one who is closest to the secret number. So I run Math.abs on the distance of both computer and human guesses to the TargetGuess. If the computer's point is higher than the human, then the human wins since it is closer to the target. When tied human wins as well. Otherwise if the computer's point is lesser than the human, computer wins. The project says to return true if human wins, to return false if computer wins.
*/
function compareGuesses(humanGuess, computerGuess, targetGuess) {
computerGuess = generateTarget();
humanPoint = Math.abs(targetGuess - humanGuess);
computerPoint = Math.abs(targetGuess - computerGuess);
console.log(humanPoint);
console.log(computerPoint);
if (computerPoint >= humanPoint ) {
return true;
} else if (computerPoint < humanPoint) {
return false;
}
}
/* Project says that we need an updateScore function that takes in only one argument named winner, which will come back as either 'human' or 'computer'. So if the winner comes as 'human' I am to increase the humanScore by 1 else if it comes as 'computer' the computerScore is to increase by 1.
*/
function updateScore(winner) {
if (winner = 'human') {
humanScore = humanScore + 1;
} else {
computerScore = computerScore + 1;
}
}
function advanceRound() {
currentRoundNumber++
}
Questions and Notes:
1- The compareGuesses() function does not appear to be running smoothly.
For instance the secret number rolls as 7.
Computer guesses 6.
I guess 1.
Yet somehow the human wins?
2- My updateScore function is not working correctly. I originally tried humanScore++ and computerScore instead of the whole humanScore = humanScore + 1; but when it failed I switched to this, alas no luck. What happens is regardless of who wins the humanScore increases by 1.
3- I’ve Inserted two console.log commands inside the compareGuesses() function to see the output of each value. However the code won’t let me see those values, so I can’t compare them with what I see on the web side. I was merely wondering if the actual numbers show up or if I somehow botched the code. Basically a debug atttempt on my end, I was simply curious why the console.log doesn’t bring anything at the code side.
4- I’ve went ahead and peeked at the correct code for it. There was a line I did not understand, perhaps you can help me?
const compareGuesses = (humanGuess, computerGuess, targetGuess) => {
const humanDifference = Math.abs(targetGuess - humanGuess)
const computerDifference = Math.abs(targetGuess - computerGuess)
return humanDifference <= computerDifference;
}
I get the humanDifference and computerDifference. However the return part confuses me. return keyword as far as I know is simply means to store data. How is
“humanDifference <= computerDifference” is simply saying that if humanDifference is less or equal to the computerDifference it is true, otherwise it is false?
Sorry if my speech is a bit complex, English is not my native language.
I appreciate all the help!