Number Guesser project help

my code link is: https://gist.github.com/485eeb02ece9687b647031ea8cd1dc6f and it is perfectly working. But I could not understand task no - 7 of this project. I am attaching the screenshot of
task no - 7.
Can anyone help me to incorporate task no-7 in my project?

the “problem” is here:

const humanDifference = Math.abs(targetNumber - humanScore);
const computerDifference = Math.abs(targetNumber - computerScore);

you essentially do the same thing twice, so it would be cleaner (and easier to test) to move this functionality to its own function:

const humanDifference = getAbsoluteDistance(targetNumber, humanScore);
const computerDifference = getAbsoluteDistance(targetNumber, computerScore);

then do math within the getAbsoluteDistance function (which you will have to make). But you already have the logic, so shouldn’t be to complicated.

this is called refactoring.

1 Like

Thanks for Your help. But I still could not get it. Would You kindly refactor it in function getAbsoluteDistance( );
also please let me know how to use alert( );

well, pretty much the logic you have here:

Math.abs(targetNumber - humanScore);

but then with the parameters of of getAbsoluteDistance function.

the function call i demonstrated in my previous answer had two arguments, thus the function should have two parameters.

there isn’t much to alert, have you checked the documentation?

https://developer.mozilla.org/en-US/docs/Web/API/Window/alert

1 Like