Number Guesser Challenge Project (JavaScript)

Here is the solution I came up with, please hit me up with things I could improve, thanks! Number Guesser · GitHub

in your compareGuesses function:

const compareGuesses = (uN, cN, tN) => {
  if (uN === cN || uN === tN){
    return true;
  }
  else if (cN === tN){
    return true;
  }

One of those return true should actually be return false

Also, you’re missing a return in the else block afterward here:

x > y ? true : false;

Hey everyone. I completed this task but I had to read the solution to get through step four. Would someone be able to help me understand why we need to declare variables before writing the “if else” statement in step 4?

Hi, i am almost completing this project, however i am facing some issues.

So when i console log my code i get 3 undefined but the Random score works fine.

When i make a guess i win, but clicking on Next round dosen’t work, i wonder what i missed?

Here is my code:

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

// Write your code below:
let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;

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

const compareGuesses = (humanGuess, computerGuess, secretNumber) =>{
  secretNumber = generateTarget();
  let human = Math.abs(humanGuess - secretNumber);
  let computer = Math.abs(computerGuess - secretNumber);
  if(human > computer){
    return true;
  } else if(computer < human){
    return false;
  }else if (human === computer){
    return 'No winner'
  }
}

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

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

// console.log(updateScore());




Hi, ok so i figured it out by doing it off platform, and adding console.log so i can see what errors are being generated by chrome.

But now i do have 1 issue and my issue is this what ever number i choose it always says I win.

Any help would be appreciated.

My previous code is now updated.

Hi,

Everything works great now, it is all fixed and working great.

Code can be seen on my Github: GitHub - mrkapable007/numberGuesser

Hello. I completed the project and wanted to share my solution in hopes that I can receive feedback to improve my coding practice. Some specific questions I have about coding in Javascript surround error handling and type checking.

Error Handling and Type Checking

I created an additional function that checks for input between 0 and 9. However, there are no checks in place for entering other than numeric data. Is there an appropriate method to handle this? The program wouldn’t work unless the input is numeric.

I appreciate any feedback so that I can improve.
Thank you.

Github Gist

Hi, this is my first post ever on a coding forum so forgive me if I come across a bit unclear. I am looking forward to participating more in future dialogue with my fellow Codecademy colleagues.

I would love to be helped with the following:

  1. I added the alert method on line 23 to alert the user if they select a wrong number and it works however I see that the program still continues to run and still counts the round as a win. I can’t seem to figure out how to stop this from happening

  2. When playing the game, the number selection box/button defaults to a blank box (as opposed to defaulting to a 0). When pressing on the “+” button it starts out 1 and in order to select “0” I have to press “+” and then “-”. I was wondering how to fix that?

You could do
return false;
on the line after alert(message_here) (but inside the same if-statement block)
to make picking a number outside of 0-9 an automatic loss.

You could set value to start with after pressing the “Next Round” button by changing a line of code in the game.js file.
Change one of the lines in the function inside of nextRoundButton.addEventListener( )
from
humanGuessInput.value = '';
to
humanGuessInput.value = 0;

1 Like

Hi, thanks for your response. I didn’t realize I forgot to attach my code and greatly appreciate that you managed to reply and solve my issues despite the lack of my code. I ended up using your solution for changing humanGuessInput to Zero. For the first problem, I decided to throw a custom error below the line with my alert (as shown below). This ended up breaking the code until the user entered and thereby forcing the user to enter the correct number between 0 and 9.

  if(humanGuess < 0 || humanGuess > 9){
    window.alert("Error! Guess must be between 0 and 9");
    throw Error("Guess must be between 0 and 9");
  } else {
      humanGuess = absoluteDistance(humanGuess);
      computerGuess = absoluteDistance(computerGuess);
      if(humanGuess === computerGuess){
      return true;
    } else if (humanGuess < computerGuess) {
      return true;
    } else if (computerGuess < humanGuess){
      return false;
    }
  }```
1 Like

Wait… Where it come from Computer Guess Random number? :disguised_face:

1 Like

There is a helper module, game.js that contains the function to do that.

1 Like

Ahw… I made my own. There’s a reason for that?

Not sure any other reason than separating as much code from the user’s editor as possible to not clutter up their page.

1 Like

Done!!
It’s working and it looks tidy :slight_smile:

I hope it is usefull for you
Any feedback is more than welcome

I HAVE A QUESTION!! I used different names for parameters
game js: compareGuesses(currentHumanGuess, computerGuess, target)
my parameters script.js : compareGuesses = (humanGuess, computerGuess, targetNumber)
The game seems to WORK WELL ANYWAY. Is this a mistake I made, or it doesn’t matter??
Thanks a lot

The names for the parameters in the functions don’t really matter.

1 Like

Hello world:
my guessing game project was so challenging particularly the second task but I was able to overcome that with the internet and stackoverflow.
looking at the solution, I saw how simple I could solve that but learning is a process.
you can checkout my page here: Number Guesser
And my code: GitHub - aladesaye/Guessing-game: My first off-platform Javascript project on codecademy.

I think my code functions correctly.
Hopefully it makes any other fellow codecademy cadets chuckle if they read it.

Hello Everyone! this is my project!

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

// Write your code below:
const generateTarget = function(){
  let randomNumber = Math.floor(Math.random() * 10);

  return randomNumber;
}

function compareGuesses(userGuess, computerGuess, secretGuess){
  if(Math.abs(secretGuess - userGuess) < Math.abs(secretGuess - computerGuess) || Math.abs(secretGuess - userGuess) === Math.abs(secretGuess - computerGuess)){
    return true
  } else{
    return false
  }
}

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

function advanceRound() {
   currentRoundNumber =  currentRoundNumber + 1;
}