 Number guesser task 7

Hello guys, I’ve read all the threads but haven’t found a final decision on task 7.
Can anyone explain how I can use the function getAbsoluteDistance() and how I can implement that function on the compareGuesses() function.
I see that we should create something like this function and what goes next?
const humanDifference = getAbsoluteDistance (argument1, argument2)
Please don’t suggest the decision like creating variables
(const humanDifference = Math.abs(targetGuess - humanGuess);
const computerDifference = Math.abs(targetGuess - computerGuess)) because I already did it myself.

Hello Welcome to the forum!

In this article -> How to ask good questions (and get good answers) you will find some helpful tips on how to help us to help you If you have a problem with this exercise - please describe the problem and post your code.

1 Like

If you already have written this function, getAbsoluteDistance that takes two parameters and returns the absolute distance then the only thing that is left to do is to use this function in the compareGuesses function.

In the compareGuesses function you probably have something similar to this:

const humanDifference = Math.abs(targetGuess - humanGuess);
const computerDifference = Math.abs(targetGuess - computerGuess);

so just change this code to get the absolute distance from getAbsoluteDistance function, isntead of calculating it (Math.abs(targetGuess - humanGuess)).

2 Likes

Thanks for the explanation.

Everything seems to be working now.

I’m going to insert my code here for two reasons: 1) people including you can leave feedback; 2) I think it can be helpful for the future cohorts who have the same questions.

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

// Write your code below:
const generateTarget = () => {
return Math.floor(Math.random() * 10)
}
console.log(generateTarget());

const compareGuesses = (userGuess, computerGuess, secretTarget) => {
const humanDifference = getAbsoluteDistance(userGuess, secretTarget);
const computerDifference = getAbsoluteDistance(computerGuess, secretTarget);
return computerDifference >= humanDifference;
}

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

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

const getAbsoluteDistance = (a1, a2) => {
return (Math.abs((a1 - a2)));
}

const alertMessage = (userGuess) => {
if (userGuess > 9 || userGuess < 0) {
alert('Guess must be between 0 and 9!');
}
};
1 Like

You’re very welcome let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;

// Called at the start of each new round in order to generate the new secret target number.

const generateTarget = () => {

return Math.floor(Math.random() * 10);

};

// This function will be called at the start of each new round in order to generate the new secret target number.

console.log(generateTarget());

const compareGuesses = (userGuess, computerGuess, targetGuess) => {

/*// Determines which player (human or computer) wins based on which guess is closest to the target.
If both players are tied, the human user should win.
// Return true if the human player wins, and false if the computer player wins.
*/

const userDifference = getAbsoluteDistance(userGuess, targetGuess);
const computerDifference = getAbsoluteDistance(computerGuess, targetGuess);

return computerDifference >= userDifference;

};

// if (userDifference > computerDifference) {

// 	return false;
// }else if(userDifference < computerDifference){

// 	return true;
// }else {

// 	return true;
// }

// This function will be used to correctly increase the winner’s score after each round.

const updateScore = winner =>{

if (winner === 'human'){

humanScore++;

}else if(winner === 'computer'){

computerScore++;
}

}

// This function will be used to update the round number after each round.

const advanceRound () => {

currentRoundNumber++;

};

const getAbsoluteDistance = (x, y) =>{

return (Math.abs((x - y)));

};

// // functionality to check whether the user guess is between 0 and 9 and alert() the user that their number is out of range.

const alertMessage = (userGuess) => {

if (userGuess > 9 || userGuess < 0){