 # Number guesser for javascript

Hello everyone,
I am currently in the middle of doing a number guesser project on javascript. In the function there are three parameters representing the user (human) guess, a computer guess, and the secret target number to be guessed. The closest guess to the target number wins. In order to evaluate this. I need to compare the absolute value of the difference between the target and each guess using the javascrip math.abs method (). I don’t know how to go about this. Any help would be much appreciated.

1 Like

Have you got any code to start us off? It might not help too much if we just gave you the answer You do understand what absolute value is, right?

In math we write,

``````|-a| == a
``````

An absolute value has no sense of direction so is a scalar. That means we can subtract b from a, or a from b and arrive at the same positive value (distance between). Reasoning this out, if we measure from point A to point B, or from point B to point A, we should expect the same measurement.

``````a = 60
b = 20
z = 35

if (math.abs(a - z) < math.abs(b - z)) {
// a is closer to z
} else
if (math.abs(b - z) < math.abs(a - z)) {
// b is closer to z
} else {
// a and b are the same distance from z
}``````
``````let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;

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

const humanGuess = (userInput)=> {
if (userInput === 0) {
return userInput;
}
else if (userInput === 1){
return userInput;
}
else if (userInput === 2){
return userInput;
}
else if (userInput === 3) {
return userInput;
}
else if (userInput === 4) {
return userInput;
}
else if (userInput === 5) {
return userInput;
}
else if (userInput === 6) {
return userInput;
}
else if (userInput === 7) {
return userInput;
}
else if (userInput === 8) {
return userInput;
}
else if (userInput === 9) {
return userInput;
}
else {
console.log('error');
}
};
humanGuess(3);
const computerGuess = ()=>  {
Math.floor(Math.random() * 9);
return Math.floor(Math.random() * 9);
};
console.log(computerGuess());
const compareGuesses = (generateTarget, humanGuess, computerGuess) => { }
``````

this is what i have so far. just having problem coming up with a way to compare the guesses with the generateTarget number using the Math.abs () method.

thanks for the response.
I do know what an absolute value is. I am having problem implementing it.
here is my code below… to give you an idea

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

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

const humanGuess = (userInput)=> {
if (userInput === 0) {
return userInput;
}
else if (userInput === 1){
return userInput;
}
else if (userInput === 2){
return userInput;
}
else if (userInput === 3) {
return userInput;
}
else if (userInput === 4) {
return userInput;
}
else if (userInput === 5) {
return userInput;
}
else if (userInput === 6) {
return userInput;
}
else if (userInput === 7) {
return userInput;
}
else if (userInput === 8) {
return userInput;
}
else if (userInput === 9) {
return userInput;
}
else {
console.log('error');
}
};
humanGuess(3);
const computerGuess = ()=>  {
Math.floor(Math.random() * 9);
return Math.floor(Math.random() * 9);
};
console.log(computerGuess());
const compareGuesses = (generateTarget, humanGuess, computerGuess) => { }
``````

If the number is limited to a range of 0 thru 9 then,

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

is how to generate it.

Testing the user input against this range should be fairly straight forward and not require as much selective code as what you have above.

``````if (userInput < 0 or userInput > 9) {
// fail user input
} else {
return userInput
}
``````

Now if we wish to exclude 0 and include 10, then,

``````Math.floor(Math.random() * 10 + 1)
``````

and the conditional check,

``````if (userInput < 1 or userInput > 10)
``````

Simplify your code and repost so we can go on to the next step.

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

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

const humanGuess = (userInput)=> {
if (userInput < 0 || userInput > 9) {
return console.log(‘error’);
} else {
return userInput;
}
};
humanGuess(3);

const computerGuess = ()=> {
Math.floor(Math.random() * 9);
return Math.floor(Math.random() * 9);
};
console.log(computerGuess());
const compareGuesses = (generateTarget, humanGuess, computerGuess) => {}

updated.
Next step is comparing guesses with generated target

Refer to the earlier example about absolute values.

The above function only needs one input… humanGuess. The other two can be generated inside the function.

``````compareGuesses = humanGuess =>
let target = generateTarget()
let computer = computerGuess()
// if statement as shown earlier
return // winner or tie
}
``````

Technically the generated values can be done by one function.

``generateRandomNumber = () => Math.floor(Math.random() * 10 + 1);``
2 Likes

Hi there! Since there is already a topic for this, I didn’t want to open a new one. Have you finished the project? I need help with it. I’m confused about what I’m doing wrong in the function that compares the numbers.

Here’s my code:

``````> const generateTarget = () => Math.floor(Math.random() * 10);
>
> const compareGuesses = (humanGuess, computerGuess, secretTarget) => {
>
>   secretTarget = generateTarget();
>   const valueH = Math.abs(humanGuess - secretTarget);
>   const valueCPU = Math.abs(computerGuess - secretTarget);
>
>   if (valueH < valueCPU) {
>     return true;
>   } else if (valueCPU < valueH) {
>     return false;
>   } else if (valueH == valueCPU) {
>     return true;
>   }
>
> }
``````

My train of thought for this is:

secretTarget generates with the function generateTarget().
valueH stores the absolute value of the humanGuess.
valueCPU stores the absolute value of the computerGuess.

Then, with the conditional statement I compare wich of valueH and valueCPU is less than to see who got closer to the secretTarget.

If valueH is less than valueCPU, human wins.
If valueCPU is less than valueH, computer wins.
Else, human wins.

But for example, I was just testing it right now and as you can see in the image that I insert in this, I got a secretTarget number 2, computerGuess number 4 and humanGuess number 6; so, the absolute value of the computerGuess is less than humanGuess, computer should win, instead it shows that the human wins.

I tried replicating this excercise in another script.js with this code:

``````> const generateTarget = () => Math.floor(Math.random() * 10);
>
> const compareGuesses = (humanGuess, computerGuess, secretTarget) => {
>
>   humanGuess = Math.floor(Math.random() * 10);
>   computerGuess = Math.floor(Math.random() * 10);
>   console.log(humanGuess);
>   console.log(computerGuess);
>
>   secretTarget = generateTarget();
>   console.log(secretTarget);
>   const valueH = Math.abs(secretTarget - humanGuess);
>   const valueCPU = Math.abs(secretTarget - computerGuess);
>
>   if (valueH < valueCPU) {
>     return true;
>   } else if (valueCPU < valueH) {
>     return false;
>   } else if (valueH == valueCPU) {
>     return true;
>   }
>
> }
>
> console.log(compareGuesses());
``````

And it works perfectly fine. I would appreciate any suggestions or observations. Thanks a lot!

I don’t see a flaw in your code that would cause the human to win in the situation you described. Is it possible that you didn’t click `Save` prior to playing that round? I do have a suggestion regarding your logic in your `if ... else if ... else if` block to determine the winner. How many conditions exist for the computer to win? Just one, correct? So, why not test for that condition only, and have a single `else` to cover the remaining conditions under which the human wins. Better still, consider this snippet:

``````const h = 1;
const c = 2;

const val1IsEqualOrSmaller = (val1, val2) => val1 <= val2;

console.log(val1IsEqualOrSmaller(h, c)); //what's the output?
``````

`> const val1IsEqualOrSmaller = (val1, val2) => val1 <= val2;`

This is a function that compares 2 parameters “val1”, “val2” and returns the result of a specified condition, which would be described as “val1 is less or equal to val2”.

Applied to `console.log(val1IsEqualOrSmaller(h, c)` the output would be `true`.

1 Like

Exactly! You can do the same thing in your `compareGuesses()` function 1 Like

This makes me think of min and max functions, or comparisons like `<` `>`

``````> target = 44
> closerThan = (<) `on` abs . (subtract target)
> 43 `closerThan` 42
True
> 43 `closerThan` 43
False
> 43 `closerThan` 44
False
> 43 `closerThan` 45
False
> 43 `closerThan` 46
True

``````

possibly silly, but also quite neat.

``````> computer = 50
> human = 38
> if computer `closerThan` human then "the computer wins" else "the hooman wins"
"the hooman wins"
``````

Oh and rather than changing your function to ignore your parameters and making up values on its own, you could make a second function that makes up values and then calls your function

1 Like