Number Guesser game, computer always wins

I’m trying to do the number guesser game:
https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-i/modules/fecp-challenge-project-number-guesser/projects/number-guesser-independent-practice
But regardless of who should win based on the guesses and the target number the computer is always given the win and I can’t figure out why.
Here is my code

let humanScore = 0; let computerScore = 0; let currentRoundNumber = 1; // Write your code below: //generates a target number to guess const generateTarget = () => { const target = Math.floor(Math.random() * 10); return target; } const compareGuesses = (human, computer, target) => { //gives a score based on how close guess is to target const calcScore = (num) => { if (num === target) { score = 0; } else if (num === (target + 1) || num === (target - 1)) { score = 1; } else if (num === (target + 2) || num === (target - 2)) { score = 2; } else if (num === (target + 3) || num === (target - 3)) { score = 3; } else if (num === (target + 4) || num === (target - 4)) { score = 4; } else if (num === (target + 5) || num === (target - 5)) { score = 5; } else if (num === (target + 6) || num === (target - 6)) { score = 6; } else if (num === (target + 7) || num === (target - 7)) { score = 7; } else if (num === (target + 8) || num === (target - 8)) { score = 8; } else if (num === (target + 9) || num === (target - 9)) { score = 9; } else { return console.log('something is wrong'); } return score; } //calculates a score for human and computer const humanTempScore = calcScore(human); const computerTempScore = calcScore(computer); // compares human score to computer score, closest to target wins (human wins: true, computer: false) if (humanTempScore <= computerTempScore) { return true; } else { return false; } } //updates the score based on who wins const updateScore = winner => { if (winner === 'human') { humanScore++; } else if (winner === 'computer') { computerScore++; } } // const advanceRound = () => currentRoundNumber++; const advanceRound = () => { currentRoundNumber++; }

I’m guessing the problem is in the compareGuesses function.
I built a test game to test it

const generateRandomNumber = () => { randomNumber = Math.floor(Math.random() * 10); return randomNumber; } const testGame = () => { human = generateRandomNumber(); computer = generateRandomNumber(); target = generateTarget(); compareGuesses(human, computer, target); } console.log(testGame());

And it returns undefined which I guess is falsy. But if i return the function like below it returns true or false.

const generateRandomNumber = () => { randomNumber = Math.floor(Math.random() * 10); return randomNumber; } const testGame = () => { human = generateRandomNumber(); computer = generateRandomNumber(); target = generateTarget(); return compareGuesses(human, computer, target); } console.log(testGame());

I’ve noticed everyone else seems to use Math.abs instead of my messy solution and I want to try that later but first I want to know why this isn’t working. Plz help!

ps, my first post in the forum, please be kind to me

Hi William,
the code you write in script.js is only part of the program. There is a bunch of prewritten code provided by Codecademy that calls the functions you write. In game.js you find this:

const humanIsWinner = compareGuesses(currentHumanGuess, computerGuess, target);
const winner = humanIsWinner ? 'human' : 'computer'

humanIsWinner should be a boolean as you can see in the second line. But you return a number.

Hi. Thanks for reply,

calcScore is a function nested in compareGuesses, it’s called to set values for humanTempScore and computerTempScore. Below that is an if statement that should return true or false based the scores, but it only returns undefined.

I tried rewriting my code with Math.abs and I still get undefined so I’m sure I’m making the same mistake. I just can’t figure out what it is.

let humanScore = 0; let computerScore = 0; let currentRoundNumber = 1; const generateTarget = () => { target = Math.floor(Math.random() * 10); return target } const compareGuesses = (human, computer, target) => { human = Math.abs(human - target); computer = Math.abs(computer - target); if (human <= computer) { return true; } else { return false; } // return 'human: ' + human + ', computer: ' + computer + ', target: ' + target; //also gives undefined } const play = () => { const target = generateTarget(); compareGuesses(3, 5, target); } console.log(play());

Oops, sorry, you’re right. I should have invested a little more time to read your code.
If you call the function compareGuesses (from the first Codebyte) seperately with 3 valid (hard coded) arguments, it returns a valid result indeed. It returns true AND false as it should.
The problem in your last Codebyte is that the function play doesn’t return anything. ‘undefined’ is returned by default by the console if there is nothing to log.
You should return the result returned by compareGuesses in your play function.

Yeah, the second one works. It was me not understanding enough about javascript to make a good test. The first one also works when tested this way but it still gives the win to the computer every time.
I will abandon it for now and hopefully stumble upon the solution at a later point. I spent most of the day yesterday on the number guesser and now that I’ve gotten it to work as it should I just want to move on.