Number guesser help

Hey there
I’ve banged my head against this project for an embarrassing amount of time. I scrapped my first draft because it just wasn’t working, likely because I started it on a day I wasn’t feeling great. Anyway, second draft is almost completely functional. Buttons all work, scores applied correctly and rounds increase. However, I’m running into a problem where, every few rounds, the declared winner is incorrect. What am I missing? Need fresh eyes. Thanks so much. :smile:

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

// Write your code below:

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

//console logs for debug console to make sure each step is working properly



function compareGuesses(humanGuess,computerGuess,targetNumber) {
    targetNumber=generateTarget();
    let humanAbs=Math.abs(humanGuess-targetNumber);
    let computerAbs=Math.abs(computerGuess-targetNumber);

    if (humanAbs<computerAbs) {
        return true;
    } else if (humanAbs>computerAbs) {
        return false;
    } else if (humanAbs=computerAbs) {
        return true;
    } else if (humanAbs=0)
        return true;
}
console.log(compareGuesses());

/*four separate scenarios, just to cover my bases:
    human has smaller absolute value and wins
    human has larger absolute value and loses
    absolute values are equal, human wins
    human guesses target number and wins*/


const updateScore = winner => {
    if (winner = 'human') {
        humanScore++;
    } else if (winner = 'computer') {
        computerScore++;
    }
}

const advanceRound = () => {
    currentRoundNumber++;
}

You could add prints in your function(s), reproduce the behaviour, and look at what you printed out for that situation where the wrong result could be observed.

It seems like my generator is returning two values. When I print that function, it gives me one number that is showing up front-end and a different number that it’s using back-end in the compare function. I’m not sure why this would happen.

1 Like

doubtful

don’t print the function, call the function and print the result

there’s no back-end, everything runs in the displayed page

maybe it isn’t


If the wrong winner is declared, then, who determines the winner? That’d be the compareGuesses function I imagine.
So you’d want to know what the situation there was. Interesting information would be what the input was, and what the output was, this lets you confirm that the function indeed behaves incorrectly and it tells you what the involved values are.

Once those prints are in place you’d load the code into a browser, open the console of the browser, and play the game until the situation happens, after which you can collect the information you’re after in the console.

You could also come up with your own inputs for your function, and this might be worth doing to some extent but reproducing the situation lets you observe without any guesswork, indeed, you might not be able to guess causes the problem.