Number Guesser Challenge Project (JavaScript)

Congratulations on completing your project!

Compare your project to our solution code and share your project below! Your solution might not look exactly like ours, and that’s okay! The most important thing right now is to get your code working as it should (you can always refactor more later). There are multiple ways to complete these projects and you should exercise your creative abilities in doing so.

This is a safe space for you to ask questions about any sample solution code and share your work with others! Simply reply to this thread to get the conversation started. Feedback is a vital component in getting better with coding and all ability levels are welcome here, so don’t be shy!

About community guidelines: This is a supportive and kind community of people learning and developing their skills. All comments here are expected to keep to our community guidelines

How do I share my own solutions?

  • If you completed the project off-platform, you can upload your project to your own GitHub and share the public link on the relevant project topic.
  • If you completed the project in the Codecademy learning environment, use the share code link at the bottom of your code editor to create a gist, and then share that link here.

Do I really need to get set up on GitHub?
Yes! Both of these sharing methods require you to get set up on GitHub, and trust us, it’s worth your time. Here’s why:

  1. Once you have your project in GitHub, you’ll be able to share proof of your work with potential employers, and link out to it on your CV.
  2. It’s a great opportunity to get your feet wet using a development tool that tech workers use on the job, every day.

Not sure how to get started? We’ve got you covered - read this article for the easiest way to get set up on GitHub.

Best practices for asking questions about the sample solution

  • Be specific! Reference exact line numbers and syntax so others are able to identify the area of the code you have questions about.


I’m pretty new to all of this, but was wondering if someone could help me with an issue I have with my code?

Essentially, I was wondering why my if statement in lines 17 - 21 didn’t work (the commented out section)? I have looked at the solution and my code now works, but I was wondering if there is a correct way to get my if statement working?

Any help would be great.


userG <= computerG is shorthand for:

    if (userG <= computerG) {
    } else {

so having both is redundant. Also, return means the function is handing back data to the caller and is done executing.

Ahhh… ok. Firstly I didn’t know you could do a shorthand if statement like that and so thanks. I actually didn’t think my code worked, but when I comment out the shorthand and use it to replace the shorthand it seems to work now. Maybe I did something strange before.

Anyhow, thanks a lot for the help!!

well, the shorthand actually consist of multiple steps, when you have if/else (like in your case) it can be shortened by using a ternary operator:

return userG <= computerG ? true : false;

the ternary operator is just a shorthand for if/else. But given a comparison also results in a Boolean value, in this case you can just do:

return userG <= computerG 

if you want something different then a Boolean value, you will need the ternary operator.

did you read what i said about return:

Ok I do know about the ternary operator, but didn’t recognise it when it was shortened even further.

I did see you statement regarding return and understand the difference between that and my if statement. Again thanks for the clarification.

I am trying the extra challenge for this project. I am attempting to create an alert if the user inputs a number less than 0 or more than 9. I have been trying for a while now but I can’t seem to figure out what is wrong with the code.

const compareGuesses = (humanGuess, computerGuess, targetNumber) => {
        let humanGuessGap = getAbsoluteDistance(humanGuess, targetNumber);
        let computerGuessGap = getAbsoluteDistance(computerGuess, targetNumber);
        if (humanGuessGap === computerGuessGap || humanGuessGap < computerGuessGap) {
            return true;
        else {
            return false;

const checkHumanGuess = (theGuess) => {
    if(theGuess < 0 || theGuess > 9){ //edited this as I pasted the wrong code earlier
        window.alert("Please make sure your guess is between 0 and 9");

This is my current implementation. I’ve also tried to have this code directly in the compareGuesses function but that didn’t work either.

Are you sure this is what you meant? If theGuess is 8 for example…

You are right, there is a mistake there. I’ve been re-writting that code so many times that I pasted the wrong version. But even when I do edit it to be if(theGuess < 0 || theGuess > 9) there still isn’t an alert popping up.

I just ran your code in place of mine, and the popup works fine when I try a number too high or too low. Can you post your entire code, or you could try running your code with the browser console open. See if there are any errors.

All I can say is :woman_facepalming:
The reason why my code didn’t work is that instead of loading the webpage with my code in it I wad loading the downloaded solution, which doesn’t have the code for the alert in it.
Lesson learned, always check which files I am actually loading :sweat_smile:

1 Like

Hello, My Number Guesser is acting weird, I noticed some unusual behavior if the target number = 1, human guess = 7, computer guess = 4 shouldn’t the computer have won? see image

This is my compareGuesses function

function compareGuesses(humanGuessInput, target, computerGuess) {
    if (Math.abs(humanGuessInput - target) <=  Math.abs(computerGuess - target)) {
        return true;
    } if  (Math.abs(humanGuessInput - target) >=  Math.abs(computerGuess - target)){
        return false;

my full code is here

Hi! I’m trying to do step 7 and create a check functionality with alert(). But it isn’t work. What is wrong with my code?

const check = (humanGuess) =>
humanGuess > 9 ? window.alert( “Your number is out of range! Pleas, choose number between 0 and 9.” );

1 Like

A ternary operator is an if/else shorthand. The else of your ternary operator seems to be missing altogether.

Ok, I changed the code on:

const check = humanGuess => {
if (humanGuess > 9) {
window.alert( “Your number is out of range! Pleas, choose number between 0 and 9.” )}

but it still doesn’t work :frowning: When I try to check the code separately it returns (alert undefined)

Not sure alert() is supported by Codecademy Learning Enviroment. Try jsbin?

Otherwise, i will need to see your full code.

I tried Jsbin this function doesn’t work.
Here’s my code:

Your check function is never called? Also, you don’t prompt the user for input, so how can they enter a number higher then 9?

Why are some of you multiplying the random number by 9 instead of doing so by 10?

return Math.floor(Math.random() * 9);

return Math.floor(Math.random() * 10);

If you multiply by 10 and then use Math.floor, you’ll get an integer between 0 and 9. Why aren’t you using 10? Doesn’t it change the result of the program?

No matter what number I put I keep on winning, my guess if I messed up my step 4 but I have no idea how to fix it.

Could someone please help me?

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

// Write your code below:
//--------------------STEP 3---------------------//
generateTarget = () => {
    return Math.floor(Math.random() * 9);
// Testing the generateTarget function

//--------------------STEP 4---------------------//
compareGuesses = (human, computer, target) => {
    if (Math.abs(human === computer)) {
        return true;
    } else if (Math.abs(human > computer)) {
        return true;
    } else if (Math.abs(computer < human)) {
        return false;
    } else if (Math.abs(human === target)) {
        return true;
    } else {
        return "Error!!! Something is wrong";
// Testing compareGuesses function

//--------------------STEP 5---------------------//
updateScore = winner => {
    if (winner === "human") {
        humanScore += 1;
    } else if (winner === "computer") {
        computerScore += 1;
    } else {
        return "Error!!! Please check your code";

//--------------------STEP 6---------------------//
advanceRound = () => {
    currentRoundNumber += 1;