Number Guesser Challenge Project (JavaScript)

Considering you are using humanGuess in statements in the body of the function, the parameter name needs to be changed:

// You wrote:
const compareGuesses = (userGuess, computerGuess, targetNum) => {

// It should be:
const compareGuesses = (humanGuess, computerGuess, targetNum) => {

Derp! Good spot. That’s what I get for coding at 3am! Thanks for your help :slight_smile:

1 Like

The code I ended up with:

Coming from the front end path, it felt like I was thrown into this project without much explanation to what node was or how to use it and how it works with VSC.
Somehow I’m supposed to use this new program on my own to do this project. It was very confusing, but I figured it out with the help of someone on the discord.

Hi, here is my code, thanks for watching!!

I wrote all of mine as functions and did a whole load of console logging at every point to verify it was working. I’ve left them in as I think to see that process is just as relevant as the answer.

I also put it on CodePen, just to try it out somewhere else

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

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

function compareGuesses(user, computer, secret) {
  const compare1 = (Math.abs(secret - computer));
  const compare2 = (Math.abs(secret - user));

  // human score is closer to secret
  if (compare1 > compare2) {
    return true;
  // computer score is closer to secret
  if (compare1 < compare2) {
    return false;
  // scores are the same distance apart
  if (compare1 === compare2) {
    return "It's a draw. The compare1 difference is " + compare1 + " and the compare2 difference is " + compare2;

function updateScore(x) {
  if (x === 'human') {
    humanScore = Number(humanScore) + 1;
    // console.log('adding to human score');
  if (x === 'computer') {
    computerScore = Number(computerScore) + 1;
    // console.log('adding to computer score');

function advanceRound() {
  currentRoundNumber = Number(currentRoundNumber) + 1;
  // console.log('updating the round number');

// console.log(generateTarget());
// console.log(compareGuesses(4, 3, 5)); // true
// console.log(compareGuesses(6, 7, 5)); // true
// console.log(compareGuesses(3, 4, 5)); // false
// console.log(compareGuesses(7, 6, 5)); // false
// console.log(compareGuesses(3, 3, 5)); // same
// console.log(compareGuesses(7, 7, 5)); // same
// console.log(compareGuesses(8, 2, 5)); // same
// console.log(compareGuesses(9, 1, 5)); // same
// console.log(updateScore('human'));
// console.log(updateScore('computer'));
// console.log(advanceRound());

Hi, I believe I have this working so I believe my code is correct - now for the real test - if someone could take a look at this and let me know if and where I can improve/correct things, much appreciated :smile:

Live site: Number Guesser
GH Repo: GitHub - CDProjects/Number-Guesser

1 Like

Hi all, please could I ask for some advice. on where I’m going wrong? From the logs I"m getting ‘1, undefined, true’ I don’t think the issue is with my updateScore. Thank you in advance!

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

// to generate the target number
const generateTarget = () => { 
    return Math.floor(Math.random() * 10)};
  //generateTarget ();

//to create human, computer and secret target number comparison

const compareGuesses = (humanTarget, computerTarget, targetNumber) => {
    if (humanTarget < 0 || humanTarget > 9) {
        return alert `number out of range`;

    } else if (Math.abs(humanTarget - targetNumber) < Math.abs (computerTarget-targetNumber)) {
        return true;
    } else if  
        (Math.abs (humanTarget-targetNumber > Math.abs (computerTarget - targetNumber))) {
            return false; 
        } else { 
            return true;

//compareGuesses ();

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

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


Hello everyone I’m trying to add the alert function to my code in the CodeCademy workspace for the moment before I move it over to VSCode. How would I go about creating a function to check the range of the users guess since it is a parameter in the compareGuesses function? The hint provided makes sense I just don’t know if I can even use the userGuess or humanGuess parameter to compare its value. Your help would be greatly appreciated thank you.

Hey, I see the issue with the updateScore function, when you return humanScore and computerScore it should be like this “return humanScore++” and "return “computerScore++”. This will increment the scores by 1. The way you have it written adds 1 and assigns the variables to that new value which we don’t want to happen since both variables change as you progress through the game.

Works well, great work!

Hey! No sure if your question was answered already. But I actually just added an additional line of code to the compareGuesses function. Essentially, i have the computer check that humanGuess is in the range, if not, the alert is triggered. If the number is in range, the rest of the if/else-if will run.

1 Like

I know someone said the issue was how you wrote the increments as += vs ++, both will work. Your logs are printing correctly. The computer is printing to the console what you are asking, the round should be 1 because the game will initialize there, the updated score is undefined because you dont have a winner , and the compare guesses will return true because at the initializing of the game neither the human or computer have a guess, making the guesses “the same” and that returns true.

Thank you I ended up figuring it out with trial and error but your solution is 100% correct that’s exactly what I did to get it to work