 # Number Guesser for Javascript

Hey, I have just started and I have a question about the Number Guesser project.

My compareGuesses function is totally different then the solution one.
But it works. So the question is, is my solution bad way doing it?

My code:

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

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

const compareGuesses = (human, computer, secret) =>{
if (Math.abs(human - secret) < Math.abs(computer - secret)){
return true;
}
if( Math.abs(human - secret) === Math.abs(computer - secret)){
return true;
}
else{
return false;
}
};

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

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

Hello, @py0633377077.

Welcome to the forums!

Your function is an acceptable way of accomplishing the task. It produces the expected outcome perfectly well. It could be refactored to be significantly more concise if you have the desire to do so. Consider this example:

``````const compareVals = (val1, val2) => val1 <= val2 //concise body arrow function

//could also be written:
/*
const compareVals = (val1, val2) => {
return val1 <= val2
}
*/

console.log(compareVals(5, 6)) //true
console.log(compareVals(5, 5)) //true
console.log(compareVals(5, 4)) //false
``````

The expression `val1 <= val2` is evaluated, and that result is returned. To apply this to your code, keep in mind that `val1` and `val2` could also be expressions. You are currently comparing expressions like this:

What happens if you do the following instead?

``````return Math.abs(human - secret) < Math.abs(computer - secret)
``````

An understanding of the above will help you to reduce the need to ever `return true` or `return false`. Doing so basically looks like this:

``````if (true) {
return true
} else {
return false
}
``````

A bit redundant wouldn’t you say?

Happy Coding!

P.S. I edited your post to format your code so that it is easier to read. For future reference, see: How do I format code in my posts?

So… I reviewed the initial post on how the function could possibly look, but to be honest I completely got stuck on how to build the “compareGuesses” function. So, now I am feeling pretty dumb at this point. How do you even come to the conclusion that the function should be structured that way (I guess I do not know how to problem solve properly)?

I came up with this and it completely broke…

``````const compareGuesses = (user, cpu, targetNum) => {
if (user === cpu) {
return 'You Win!';
} else if (targetNum >= Math.abs(user - targetNum) || targetNum < Math.abs(user - targetNum)) {
return true;
console.log('You Win!');
} else if (target >= Math.abs(cpu - targetNum) || target < Math.abs(cpu - targetNum)) {
return false;
console.log('Computer Wins!');
} else {
return 'Error';
}
};
``````

… I kept getting my return value for " Error" .  One suggestion in the narrative is to abstract away the absolute difference to be a helper function.

``````const absDiff = (a, b) => Math.abs(a - b);
``````

We’ll come back to this once we deal with the question, if the user wins on a tie, then do we need that conditional? Once we know the computer does not win we can default to the user.

``````    if (absDiff(cpu, targetNum) < absDiff(user, targetNum)) {
return // ...computer wins
}
return // ...user wins
``````

Have you caught on yet that anything after `return` is unreachable? It’s like the line never existed.

Hello All!
I took a little break from learning (which was bad on my part); I was just getting really frustrated and burnt out from the learning and projects. BUT I have returned back to my lesson! Trying to finish up this project in particular now, and after spending two days with it I feel like I have made some progress! I wanted to get another pair of eyes on my code. I am not getting the results I require to complete the project, so any suggestions would be greatly appreciated.

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

// Random number generator: 0 - 9 / function one
const generateTarget = () => {
let randomNum = Math.floor(Math.random() * 10);
return randomNum;
};

const compareGuesses = (user, computer, target) => {
if (Math.abs(user - target) <= Math.abs(computer - target)) {
return 'User Wins!';
} else if (Math.abs(user - target) < Math.abs(computer - target)) {
return 'User Wins!';
} else if (Math.abs(computer - target) < Math.abs(user - target)) {
return 'Computer Wins!';
} else {
return 'Error!'
}
};

// Increases the winner's score after each round / function three
const updateScore = () => {
humanScore = ++0;
computerScore = ++0;
return `Score: User has \${humanScore} points. Computer has \${computerScore} points.`;
};

// Updates the round number after each round / function four
const advanceRound = () => {
currentRoundNumber = ++1;
return `Round: \${currentRoundNumber}`;
};

console.log(compareGuesses(5, generateTarget(), generateTarget()));