 # Challenge Project: Number Guesser Step # 8: replacing Math.abs()

Hello everyone, a newbie in Javascript here having an issue.
Well, the step #8 in this project advises to create a separate function to calculate the distance between the computer guess to the target and the human guess to target.
I’ve seen that most fellow learners create a separate function just to use again the Math.abs(), which I honestly think, don’t make a difference.

I wanted to practice even further and see if we can actually re-create Math.abs() with the function I called getAbsoluteDistance(), however, the new code I made seems to partially work, as the computer always win.

I appreciate any feedback.

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

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

//This is a new function created to replace Math.abs()
const getAbsoluteDistance = (numberA, numberB) => {
let numberC = numberA - numberB;
if (numberC < 0) {
return numberC *= -1;
} else {
return numberC;
}
}

const compareGuesses = (userGuess, computerGuess, secretTargetGuess) =>  {
getAbsoluteDistance(userGuess,secretTargetGuess) <= getAbsoluteDistance(computerGuess,secretTargetGuess) ? true : false

/*Below piece of code is the original step #4, it's commented out to create and absolute distance function getAbsoluteDistance() instead of Math.abs():

Math.abs(userGuess - secretTargetGuess) <= Math.abs(computerGuess - secretTargetGuess) ? true : false;*/
}

let updateScore = winner => winner === 'human' ? humanScore += 1 : computerScore += 1;

const advanceRound = () => currentRoundNumber += 1;

``````

x The `getAbsoluteDistance` function seems fine to me.
But,
the `compareGuesses` may have an issue …
the way you have the function written now with `{` and `}` , you may need to put in return in the function.

``````const compareGuesses = (userGuess, computerGuess, secretTargetGuess) =>  {
return getAbsoluteDistance(userGuess,secretTargetGuess) <= getAbsoluteDistance(computerGuess,secretTargetGuess) ? true : false ;
};
``````

alternatively, you could do:

``````const compareGuesses = (userGuess, computerGuess, secretTargetGuess) =>
getAbsoluteDistance(userGuess,secretTargetGuess) <= getAbsoluteDistance(computerGuess,secretTargetGuess);
``````
const getAbsoluteDistance = (numberA, numberB) => { let numberC = numberA - numberB; if (numberC < 0) { return numberC *= -1; } else { return numberC; } }; console.log(getAbsoluteDistance(2,5)); const compareGuesses = (userGuess, computerGuess, secretTargetGuess) => { return getAbsoluteDistance(userGuess,secretTargetGuess) <= getAbsoluteDistance(computerGuess,secretTargetGuess) ? true : false ; }; console.log(' '); console.log(compareGuesses( -1, 2, 0));
1 Like

Thank you so much @janbazant1107978602 , this single missing return fixed the issue. 