Number Guesser Challenge Project (JavaScript)

Hey guys, here is my solution.

The only error I have, is that when I coded in the alert for when a user selects a number greater than 9 or less than 0, it will award a win instead of halting the round. Can anyone point me in the right direction for a fix for this?

Thanks in advance!

I ran into the same issue, and I was reading most of the comments under this topic to find the solution to that, but seems like everyone had problem with that. I might be mistaken but I think this is because in game.js it has its own flow. When we press button ‘make a guess’ it calls a function from ‘game.js’ file. I have marked the function (with red in a snippet) which shows the flow when the button is clicked.

as you can see in this function it calls the function ‘compareGuesses’ which we have in the ‘script.js’ . as you know it , it has to return some value (true or false). I have experimented and checked with console.log(compareGuesses(10,7,2)) image
; what happens after I click Ok in alert message, it returns ‘undefined’ value , which was mention in one of the lessons (as I remember) and it means falsy. As it false value, you can see it from the snippet, it just goes to else statement and prints : ’ Computer wins! '.

I hope that helps a little to understand the logic, sorry that this is a bit messy

Hi, i’m pretty new at this.
Can anyone tell me if i did anything wrong?

Thanks dude. I’ve read all the code in game.js. You are right. Thank You!

I’ve added the red marked if-block inside the game.js file and now everything is working perfectly.

1 Like

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

// Write your code below:

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

const compareGuesses=(human,computer,secret) =>{
let humanDifference = Math.abs(secret-human);
let computerDifference = Math.abs(secret-computer);

return humanDiferrence <= computerDifference ;

};

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

Greetings,

I’m working this project and am trying to understand every bit of this code. I completed the project and it seems to work fine but to better understand each part I deconstructed the code until I have the bare minimum to allow the program to run. Surprisingly, the code still ran fine without declaring the variable winner. Why is this so? How does the compareGuesser function tie with the updateScore function without declaring a variable such as winner? See a copy of my code below.

let humanScore = 0;

let computerScore = 0;

let currentRoundNumber = 1;

// Write your code below:

const generateTarget=() => {

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

};

let humanGuess = 0;

let computerGuess = 0;

let targetNumber = generateTarget();

const compareGuesses =(humanGuess, computerGuess, targetNumber) => {

let humanguessDifference = Math.abs( humanGuess - targetNumber);

let computerguessDifference = Math.abs( computerGuess - targetNumber);

if ( humanguessDifference <= computerguessDifference) {

return true

} else {

return false

}

};

/let winner = compareGuesses(humanGuess, computerGuess, targetNumber) ? ‘human’ : ‘computer’;/

/*if (compareGuesses(humanGuess, computerGuess, targetNumber)){

let winner = ‘human’

} else {

let winner = ‘computer’

}; */

const updateScore = (winner) => {

if(winner === ‘human’){

return humanScore += 1;

}

if (winner === ‘computer’) {

return computerScore += 1;

}

};

const advanceRound = () => {

return currentRoundNumber += 1;

};

I can’t seem to find the share button for github? Can you please advise?

Thanks

1 Like

Hello everyone,

first time here so… there’s my code:

Hey @benramsey4815384562,

I just saw your post and I think I might have an answer. This part is handled in the game.js file you can see if you download the resources. In line 30/31 they call the compareGuesses() function and save the winner,

// Determine if the human or computer wins:
const humanIsWinner = compareGuesses(currentHumanGuess, computerGuess, target)
const winner = humanIsWinner ? ‘human’ : 'computer’

…after that they call updateScore with the corresponding string in line 35.

// Update the correct score:
updateScore(winner);

So there is no need to do these steps in your code. It’s already been taken care of :wink:

1 Like

Hi everyone,
From what I’ve seen I’ve not used the most efficient method but it’s what I came up with off the top of my head! It seems to work okay. Check it out! Any suggestions are welcomed Thanks!

Game
https://htmlpreview.github.io/?https://github.com/AidanKLee/GuessingGame/blob/main/index.html

Code

Efficiency is the least concern when writing for the browser. User interaction generally takes very little code, and it is usually only one action at a time. The computer can respond very quickly no matter how inefficient the code is. There isn’t much code, to begin with, and there usually is no computing involving algorithms and complex loop structures. It’s mostly just listening for user events and responding to each as they arise.

Bottom line, the most important concern is that the code works correctly so that the user gets the experience they expect or anticipate intuitively given how the page informs them.

1 Like

After looking at the solution I realized I went a little overboard in how the compare guesses function chooses whats true or false based on the answers from the computer and user. (oh well).

Hello everyone, for the last step of checking if the user input is in range between 0 to 9 and alerting users, is there a way to stop the program from continue to guess the winner if the input falls out of range. After all if the input is out of range what’s the point of guessing a winner. I tried this logic but it still guesses. Any solution out there?

const compareGuesses = (userGuess, computerGuess, targetNumber) => { if (userGuess < 0 || userGuess > 9) { //Alert to prompt users if input is out of range. return window.alert('Please enter a number between 0 and 9'); } else { let userDifference = getAbsoluteDistance(userGuess, targetNumber); let computerDifference = getAbsoluteDistance(computerGuess, targetNumber); if (userDifference < computerDifference) { return true; } else if (computerDifference < userDifference) { return false; } else { return true; } } }

https://gist.github.com/e8992f7bc46e445289c48b10a60c7fbf

Hi there,
This is my code for this project, any feedback would be much appreciated!

please can somebody help me i had finis my so i want know all my is right sorry for english the language i speak is french

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

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

}
console.log(generateTarget())

const compareGuesses = (humanGuess, computerGuess, secretTarget) =>{

if(Math.abs(humanGuess - secretTarget) < Math.abs(computerGuess - secretTarget)){
return true;

}else if(Math.abs(humanGuess - secretTarget) > math.abs(computerGuess - secretTarget)){
return false;
}
const getAbsoluteDistance = (num1, num2) => {
return Math.abs(num1 - num2)
}

}

const updateScore = (winner) =>{
if(winner === ‘human’){
humanSocre ++;
}else if(winner === ‘computer’){
computerScore ++;
}
console.log(humanScore());
}

const advanceRound = () => currentRoundNumber ++;{
console.log(advanceRound());
}
const check = (userGuess) => {
if(userGuess > 0 || userGuess > 9){
return win;
}else if(userGuess < 0 || userGuess < 9){
return alert()
}
}

looks good mostly …

in the generateTarget function, I think that
Math.floor(Math.random() * 9)
should be
Math.floor(Math.random() * 10)
otherwise it probably would not generate a 9.

You defined the getAbsoluteDistance function inside the compareGuesses function;
which is not wrong, but it is different from others’ projects.

There’s a spelling error in the updateScore function:
humanSocre ++;
should be
humanScore ++;

In the check function,
if(userGuess > 0 || userGuess > 9){
should be
if(userGuess < 0 || userGuess > 9){

In your check function, you don’t need the return in front of alert()
because alert() does the pop-up thing by itself.
Although you may want to put a string argument for alert(),
like alert("invalid input") or something.

Here is my code
numberGuess (github.com)