# Number Guesser Challenge Project (JavaScript)

Hello Everyone! this is my project!

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

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
}
}

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

currentRoundNumber =  currentRoundNumber + 1;
}
``````

This went a little easier than expected. The one part that did trip me up a little was ensuring that when testing the guesses my result was always a positive number. I almost went down the rabbit hole of if else statements.

https://robert-gueydan.github.io/number-guesser-starting/

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

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

//compare computer vs human guess to the secret number and choose a winner
const compareGuesses = (humanGuess, computerGuess, secretNumber) => {
if (humanGuess > 10){
alert('Choose a number between 1-10 Next time');
}
let computerCompare = Math.abs(secretNumber - computerGuess); //returns the difference as an absolute number
let humanCompare = Math.abs(secretNumber - humanGuess);
if ((humanCompare === computerCompare) || (humanCompare < computerCompare)) {
return true;
} else {
return false;
}
}

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

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

``````
2 Likes

Hi everyone! New to the forum but hope I’m posting in the correct place. I am have some difficulties in understanding parts of the Number Guesser challenge. More specifically this part of the solution (task 2):

``````const compareGuesses = (humanGuess, computerGuess, target) => {
const humanDiff = Math.abs(target - humanGuess);
const computerDiff = Math.abs(target - computerGuess);
if (humanDiff <= computerDiff) {
return true;
} else {
return false;
}
}
``````

I had to look at the solution because I was stuck trying to specify the value of ‘target’ which does not seem to be necessary according to the solution? I completed the rest of the tasks but it bothers me that I don’t understand this part.

Max

The value of `target` is specified in the other JavaScript file, `game.js`

so you just have to make a function to compare it to the user’s guess and computer’s guess and indicates which one’s closer (by returning `true` or `false`).
This function is called in `game.js` too.

Ok got it! Appreciate the response!

Hi everyone, beginner programmer here!

Below is my solution for this project, if anyone has time to give me some feedback I would really appreciate it. Thank you!

Hi All,

Here is my number guessing game.

https://lkkiselov.github.io/nuber-guessing-game/

LK

The solution
let humanScore = 0;

let computerScore = 0;

let currentRoundNumber = 1;

//function to get random generated number.

function generateTarget(){

``````let targetGuess = Math.floor(Math.random() * 10);

return targetGuess;
``````

}

//comparing the guesses to the target gues to see which on was closest.

function compareGuesses(humanGuess, computerGuess, targetGuess){

``````const humanD = Math.abs(targetGuess - humanGuess);

const computerD = Math.abs(targetGuess - computerGuess);

if (humanGuess === targetGuess || computerGuess === targetGuess){

//human player wins

return true;

}

if (humanD < computerD){

//human player wins

return true;

}

if (computerD < humanD){

//computer player wins

return false;

}
``````

}

//updating the score using a function

``````if (winner === 'human'){

humanScore += 1;

}else{

computerScore += 1;

}
``````

}

//function for increasing the round number of the game.

``````currentRoundNumber += 1;