Random Number Game

Hello, I’m currently doing the random numbers game:
https://www.codecademy.com/practice/projects/number-guesser-independent-practice

This is my code below

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

//write your code here

const generateTarget = () =>
Math.floor(Math.random() *10);

const getAbsoluteDistance = (a, b) => {
return Math.abs(a - b);

}

const compareGuesses = (humanGuess, compGuess, targetGuess) => {

const humanDiff = getAbsoluteDistance(huamnGuess, targetGuess);
const compDiff = getAbsoluteDistance(compGuess, targetGuess);

return humanDiff <= compDiff ? true : false;

};

const updateScore = winner => {

if (winner === ‘human’) {
humanScore++;
} else if (winner === ‘computer’) {
computerScore++;
}
};

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

I originally had it working but I’m trying to do the extra step:

  • You probably calculated the distance from the computer guess to the target and from the human guess to the target. Move this into a separate getAbsoluteDistance() function that takes two numbers and returns the distance, and then use that inside your compareGuesses() function.

I am completely stuck as to why any code is not working, any help?

bangs head against the desk

nice little spelling mistake ‘huamn’

works fine now

Not sure if this will fully solve it but you’ve got a typo

const compareGuesses = (humanGuess, compGuess, targetGuess) => {

const humanDiff = getAbsoluteDistance(huamnGuess, targetGuess);

edit: oh yep, you’ve seen it already :slight_smile:

Thanks man, haha I can’t believe I didn’t spot it first time

Sometimes it helps to use the console when running the application. So when I copied your code into app.js and opened up index.html, this appeared in the console:

image

That then helps to troubleshoot any issues.

Ahh right, that is much more helpful thanks!

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.