Number Guesser Challenge Project (JavaScript)

function generateTarget() {
    return Math.floor(Math.random() * 10);

function compareGuesses(humanGuess, computerGuess, targetNumber) {
    return Math.abs(targetNumber - humanGuess) <= Math.abs(targetNumber - computerGuess);

function updateScore(winner) {
    winner === 'human' ? humanScore++ : computerScore++;

function advanceRound() {

how can i add the extra tasks on my code? i mean, where does humanGuess come from?

Try just ‘alert([message])’, not ‘window.alert([message])’?

Here is my Gist. I did complete it then started messing around and messed up something. So I restarted from scratch.

The second time around was much faster and has cleaner code.

Hi all,

There’s no one part of the code that just doesn’t make sense to me. If anyone could answer it, it would be amazing:

    const compareGuesses = (humanGuess, computerGuess, targetGuess) => {    // This line is fine.
      const humanDifference = Math.abs(targetGuess - humanGuess)
      const computerDifference = Math.abs(targetGuess - computerGuess)
      return humanDifference <= computerDifference;

In the second line, should humanGuess not be defined first? Otherwise, how would the computer know what the human is inputting. Same for the third line; should ComputerGuess not be defined first?

Lastly, on the the third line, why is the difference being worked out. How does that help later on with whether computerDiffrence is closer to the targetGuess than humanDiffrence? I may be wrong but since return stores things into memory, is it a way of keeping a track of the score? If that is the case. Then why isn’t their one for return computerDifference <=humanDifference?

Given, human, computer, and target, we call the function to arrive at a winner. Assume the above values are all valid.

console.log(compareGuesses(human, computer, target))

Those three arguments are what define humanGuess, computerGuess and targetGuess and supply their values.

Now consider a number line…

-9 -8 -7 -6 -5 -4 -3 -2 -1  0  1  2  3  4  5  6  7  8  9

If we attempt to subtract a number from the right side of another number, the result will be on the negative side of zero.

6 - 8 =>  -2

When we remove the sign the value is the same as if we subtract in reverse.

|-2| === 8 - 6    //  true

An absolute value is a scalar (a value with no direction component). Knowing that we can do this is at the heart of the of our comparison.

let computer = 8
let human = 3
let target = 5

    5 - 8 => -3
    5 - 3 =>  2

    |-3| > 2

Human wins.