Number Guesser Challenge Project (JavaScript)

This is my code, any feedback is welcome!

Thank you!

Hi! I believe it is because your inputs are in the incorrect order. It should go human guess, computer guess, and then target. The game.js code calls out the functional and enters them in that order. That may be why it is wrong.

Hello! Here is my completed project with the extra two features implemented. Not sure if did it with perfect syntax, but as far as my testing shows it works well! Please feel free to let me know if there are any alterations. Note that some of my alterations were made in the game.js file between lines 34 and 52.

One thing I saw in a previous comment here that was pretty cool was a shorthand for the if statement on 14-18. You can actually replace the whole thing with:

return userDelta <= computerDelta;

This comparison returns a boolean value, so it will return true or false without the if statement. You can also use a ternary operator for it ( I kind of forgot about those until reading in this forum):

if (userDelta <= computerDelta) ? true : false;

Your if statement in updateScore could also be formatted easily as a ternary operator:

if (winner === ‘human’) ? humanScore++ : computerScore++;

Looks good! Similar to mine :sunglasses:

Thank you for your hints and comments, very much appreciated. I actually saw the one regarding the truthy value as well after I had publshed my piece of code - but didn’t want to change mine after the fact although this is certainly the more elegant solution. The ternary operator still looks somewhat less “readable” to me but this surely something to get used to.

Thanks again :clap:

1 Like

Here is what I came up with. I hope this helps anyone having issues on this challenge project! Please use this and learn from it! I worked my butt off to do this one on my own!

I’d like to point out a flaw in how the student is instructed to create the updateScore() function in Step 5.

Up until Step 5, the student has been told to create functions seemingly without creating any new global variables other than the three predefined ones at the top, and he or she would have maintained the same assumption when the student creates the advanceRound() function in Step 6, where surprisingly, zero parameters are required. (This is unlikely if the student is to create the script for the game from scratch, but I digress.)

In my experience, Step 6 implies that some other JS files (in our case, game.js) are working in accordance with our own script.js file, therefore, no parameters are required to make that function work, as long as the function has the necessary statements to increment the variable currentRoundNumber. I’m okay with that.

But back to the updateScore() function in Step 5. It seems to me that there is no way to make it work unless there is some way to inform updateScore() the winner of the round. What I did was make a global variable so that the compareGuesses() function will update the variable, and in turn the updateScore() function will execute accordingly.

However, upon looking at the hint after Step 7 as well as Codecademy’s own source code for the game, it is apparent that the student is assumed to know that there is this winner variable with two possible arguments, namely human and computer, in game.js! But this assumption on the student is a contradiction to Step 2 where the student is told not “to look at game.js and shouldn’t edit it if (the student wants his or her) project to work as intended”! This certainly has crated some confusion for students, at least, for me.

Anyway, I am really happy that I joined Codecademy Pro and I think you guys did a great job creating the courses and practice projects. I hope that my feedback above would improve what are already excellent resources for programming beginners! Thank you!

Here’s my crack.

I coded out normally, then changed it to use short functions and ternaries.

I added the alert() for when a player chose a number outside of parameters to the game.js file, in the click event function.

Any tips welcomed

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

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

const compareGuesses = (currentHumanGuess, computerGuess, target) => {
  const humanAbs = Math.abs(currentHumanGuess - target);
  const computerAbs = Math.abs(computerGuess - target);
  return humanAbs <= computerAbs;
};

const updateScore = winner => winner === 'human' ? humanScore +=1 : computerScore +=1;

const advanceRound = () => currentRoundNumber += 1;

Hi everyone,
For this task you can find my solution on github with this link :

Please feel free to leave a comment, thanks!
Enjoy :wink:

This is my solution with the two other extra practice features.
Hope this helps someone!

I don’t understand line 8: return Math.floor(Math.random() * 9) in the solution.
I thought Math.round() returned a number between 0 and 1, 1 not included, so if we multiply it by 9, then 9 wouldn’t be possible either? And then we use Math.floor which rounds to the nearest lower integer, so the result should be [0; 8]. Please, can someone explain what I’m missing?

Hey all!

Here’s my first attempt at the Number Guesser Challenge. My solution seems to be a bit less clean than the solution Codecademy provided, but a solid first effort.

Hey @board8101805364,

I managed to fix this issue by adding a return statement right after the alert function.

if (currentHumanGuess < 0 || currentHumanGuess > 9) {
alert(‘Please enter a value between 0 and 9.’);
return;
}

Hey guys! this is my code, so far it seems to work properly…

codeCademy/script.js at master · sebastian1134/codeCademy (github.com)

Do you know why this code may not be working? Everything works properly except the Target Number: undefined

Any idea?

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

// Write your code below:
const generateTarget = () => {
target = Math.floor(Math.random() * 10);
};

const compareGuesses = (human,computer,target) => {
human = human - target;
computer = computer - target;
if (human < 0) {
human = human * -1;
} else human;

if (computer < 0 ) {
computer - computer * -1;
} else computer;

if (human < computer || human === computer && computer !== 0) {
winner = ‘human’;
return true;
} else winner = ‘computer’;
return false;
};

const updateScore = (winner) => {
if (winner === ‘human’) {
humanScore += 1;
} else computerScore += 1;
};

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

Hi,

I’m not following you when you say “return means the function is handing back data to the caller and is done executing.” What should I review to come to this same understanding?

Thank you.

Hey guys have a question about executing the alert() prompt. Currently I have the alert coming up, but the program will still execute and assess the Round Winner.

If I wanted to amend it to stop the Round so the user may change the input, how would I go about doing that? I cant think of a way to stop the Round from executing without altering script in game.js

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

// Write your code below:
const generateTarget = () => {
    return Math.floor(Math.random()*10);
}

const getAbsoluteDistance = (num_1, num_2) => {
    return Math.abs(num_1 - num_2)
}

const compareGuesses = (user, computer, answer) => {
    
    if (user < 0 || user > 9) {
        alert('Choose a number between 0 and 9');
    } else {

    }

    if (getAbsoluteDistance(user, answer) > getAbsoluteDistance(computer, answer)) {
        return false;
    } else {
        return true;
    }

}

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

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

not sure, that was the explanation. maybe this mdn explanation helps?

return does literally what is says: return(ing). When a return keyword/statement is reached, the data is handed back (returned) to the caller/function call. The rest of the function is then no longer executed. Kind of like making an exam, you can decide to hand back (return) the exam to the teacher even without having filled in all the questions (the entire body of the function having executed)

hey peeps!
Can some one help me I’m struggling to find out why my code isnt working. the error states there is a unexpected token however i can’t seem to find it. Also im new to coding and not really good if at all

Thankyou for the help!

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

// Write your code below:
function generateTarget = () => {
let target = Math.floor(Math.random() * 10);
console.log("target: ", target);
return target;
};
const compareGuesses = (human, computer, target) => {
const humanGuess = Math.abs(target - human);
const computerGuess = Math.abs(target - computer);
if (humanGuess < computerGuess) {
return “Human won this round!”
} else if (humanGuess > computerGuess) {
return “Computer won this round!”
} else {
return “Computer won!”
}
};

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

let advanceRound = () => {
currentRoundNumber += 1;
};

updateScore(‘human’);
console.log(humanScore);
updateScore(‘computer’);
console.log(computerScore);

Hi There,

I am new here, my code is working but keeps saying “Target Number: Undefined”. Can someone help?

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

let generateTarget = () => {
  Math.floor(Math.random()*9);
};
let compareGuesses = (humanGuess, computerGuess, targetNumber) => 
{
  const humanDifference = Math.abs(targetNumber - humanGuess);
  const computerDifference = Math.abs(targetNumber - computerGuess);
  if (humanDifference <= computerDifference) {
    return 'Human won!!!'
  } else if (humanDifference > computerDifference) {
    return 'Computer won!!!'
  }
};
let updateScore = (winner) => {
if (winner === 'human') {
  humanScore += 1;
} else if (winner === 'computer') {
  computerScore += 1;
}
};
const advanceRound = () => {
  currentRoundNumber += 1;
};