Javascript Number Guesser project

Hello friends!

I’m taking the web development career path and up until now I’ve had a relatively easy time navigating the course work and material.

I’m currently working on the independent challenge; https://www.codecademy.com/practice/projects/number-guesser-independent-practice ;

I’ve gotten by well just surfing the forums and finding threads of other peoples problems. It’s helped me resolve most of the issues I have faced. Though sometimes it feels like cheating because I’m referencing already completed work.

I’d like to screenshot my code and then have someone give some insight as to why it may be that whenever I play the game on Codecademy’s local host, the computer always wins.

In the screenshots above you can see that the ‘target number’ is 5, my choice was 5, and the computer still wins. What have I done wrong?

Hi @cmokbel could you please post code using the , you know the thingy that makes it clear to see, it’s like all blurry ,

Your compareGuesses function doesn’t meet the requirements from the instructions. Instead of taking 3 parameters, you are only taking humanGuess as a parameter, and then generating new values for the secret number and computer guess instead of using the numbers sent to your function when it is called by code that you don’t see in another file called game.js. Essentially, you’re trying to do too much. If you stick to doing only what the instructions call for, you’ll have better luck. You can view the code in game.js by clicking the little folder icon in the top left corner of the code editor, and selecting game.js from the list. Altering the code there will mess up the project though, so be careful.

Your code should only contain the functions. The functions are called from game.js.

Also, you are making an awful lot of comparisons in your compareGuesses function to determine if 1 number is less than or equal to another number. A single comparison will do.

1 Like

@_max sorry, I used print screen to capture the photos, I hope this one is better.

@midlindner I will try and reconstruct the functions, thank you for your input!

1 Like

Posting your correctly formatted code directly on to the forum is hands down the best approach, in no small part because it means we can copy it and run it ourselves to recreate the issue. :slight_smile:

1 Like
const humanGuess = (number) => {

    if (number < 0 || number > 9) {

        return 'Error, Please try a number between 0 and 10!';

    } else {

        return number;

    }

}

// generating the target number to match

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

}

// generating the computer's guess

const computerGuess = () => { Math.floor(Math.random() * 10); return Math.floor(Math.random() * 10);

}

// comparing the human guess to the target and computer guess

function compareGuesses(humanGuess) {

    let secretNumber = generateTarget();

    let computer = computerGuess();

    if (Math.abs(humanGuess - secretNumber) <= Math.abs(computer - secretNumber)) {

        return true;

    } else if (Math.abs(computer - secretNumber) < Math.abs(humanGuess - secretNumber)) {

        return false;

    } else if (Math.abs(humanGuess - secretNumber) === Math.abs(computer - secretNumber)) {

        return true;

    } else if (humanGuess === secretNumber) {

        return true;

    } else {

        return false;

    }

    }

    // updating the score for each round won

const updateScore = (winner) => {

 if (winner === 'human') {

     humanScore ++;

 } else if (winner ==='computer') {

     computerScore ++;

 }

}

// increase the round number by 1

const advanceRound = () => {

    currentRoundNumber ++;

  }

Unless you’ve altered the game.js file, or have somehow modified the overall project to not use it, you have too many functions. You are only supposed to write 4 functions. The code in game.js handles calling the functions including generating the computer guess. Your code is generating a new target and new computer guess that are not the same as those displayed on the screen. You are looking at the apples, but comparing oranges under the hood.

1 Like

@midlindner So, I removed the let ‘computer’, and let ‘secretNumber’. I also removed the generation of the numbers from my code as it is already called in the game.js. Now however, my code does not run.

Got it! Sweeeeeeeeeeeeeeeeeet, time to go to work! yes! what a great way to end the day of coding!!!

1 Like