Number Guesser

Hello, My Number Guesser is acting weird, I noticed some unusual behavior if the target number = 1, human guess = 7, computer guess = 4 shouldn’t the computer have won? see image

This is my compareGuesses function


function compareGuesses(humanGuessInput, target, computerGuess) {
    if (Math.abs(humanGuessInput - target) <=  Math.abs(computerGuess - target)) {
        return true;
    } if  (Math.abs(humanGuessInput - target) >=  Math.abs(computerGuess - target)){
        return false;
    }
}

my full code is here https://gist.github.com/JamesCodes0/344d46802460818cac0665f25d301433

the numbers provided to your function are 7 4 1, which is a difference of 3 each way, a tie, defaulting to the human player winning

so then, where is your mixup? (you will need to compare what I just said to what was supposed to happen, what part of that is wrong? it might elude you if you just stare at it and try to figure out what’s wrong. you will need to consider what should happen, what would have led to a different answer, and how is yours different from that?

though, I’m not saying anything that doesn’t say in your code, so really it is the events in your code that you should be comparing to what should have happened.

The Target number is 1 .
The human guess is 7. 1 - 7 = -6 = absolute value of 6
The computer guess is 4. 1 - 4 = -3 absolute value of 3
The computer guess is closer. yet the human won… i don’t think this is as intended.

Yeah that’s what’s wrong with what I said.
And I said what your code thinks.
So therefore, what needs to change?

You can verify that this is the problem before making any further considerations:

console.log <-- use me

I have put in console.logs into my function to see the outputs and i still do not understand why the code is determining the wrong winner.


hint would be greatly apprecaited. thanks

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

// Write your code below:
function generateTarget() {
    return Math.floor(Math.random() * Math.floor(9));
}

function compareGuesses(humanGuessInput, target, computerGuess) {
    let humanDiff = (Math.abs(humanGuessInput - target))
    let compDiff = (Math.abs(computerGuess - target))
    console.log('Human Difference ' + humanDiff)
    console.log('Computer Difference ' + compDiff)
    if (humanDiff <= compDiff) {
        return true;
    } if  (humanDiff > compDiff) {
        return false;
    }
}
console.log(compareGuesses(9,1,8))

function updateScore(winner) {
    if (winner == 'human') {
        return humanScore++
    } if (winner == "computer") {
        return  computerScore++
   } else {

}
}

function advanceRound() {
    return currentRoundNumber++
}

You’re not reproducing the problem when doing that though. Because you’re calling the function yourself, and you’re not calling it the same way.
You’d need to look at the console output from when you run it in a website, and call the function indirectly by clicking the buttons in the website.

It’s a trivial bug and it might seem dumb but all the more reason why you should be able to track it down. So reproduce it and then follow the problem all the way to the source. It’s not guesswork or being able to write things correctly from the start or being able to somehow see it by staring… reproduce it and observe, that’s the most reliable tool you’ve got.


i was able to see the outputs in the console. but i am still baffled as to what is wrong.

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

// Write your code below:
function generateTarget() {
    return Math.floor(Math.random() * Math.floor(9));
}

function compareGuesses(humanGuessInput, target, computerGuess) {
    let humanDiff = (Math.abs(humanGuessInput - target))
    let compDiff = (Math.abs(computerGuess - target))
    console.log('Human Difference ' + humanDiff)
    console.log('Computer Difference ' + compDiff)
    if (humanDiff < compDiff) {
        return true;
    } if  (humanDiff > compDiff) {
        return false;
    } if (humanDiff == compDiff) {
        return true;
    }
}

function updateScore(winner) {
    if (winner == 'human') {
        return humanScore++
    } if (winner == "computer") {
        return  computerScore++
   } else {

}
}

function advanceRound() {
    return currentRoundNumber++
}

Are those results in the console correct?
What was done to obtain them?
Do those numbers relate in any way to the numbers in the page?

Might want to look at what things are like at the start of the function, the input, and what the output is. Then you’d be able to determine whether the problem is before your function, in your function, or after the function.

I was able to fix it. Thanks for your help @ionatan

for anyone else having issues here is my working code.
you can demo it here