# Question 7 on the Number Guesser project

Those functions were never called for. The function we are asked to write is meant to be in the script.js file, not the game.js file. It is to be one function for both purposes.

Its question 7 on the number guesser challenge project, which is an extension question. I have completed the project, but I am stuck on the extension question.

You probably calculated the distance from the computer guess to the target and from the human guess to the target. Move this into a separate `getAbsoluteDistance()` function that takes two numbers and returns the distance, and then use that inside your `compareGuesses()` function.

Notice it only asks for the one function? Then when comparing the guesses we will make two calls, one with human, target, and one with computer, target. We compare the two return values.

Is it possible to use two return keywords, returning two values for each one? Or do I rewrite the functions, creating a lone function, and use one return keyword and giving two values?

… and make two calls.

1 Like

Ohh yes. I remember now. Ill try it and I’ll get back to you if I have any issues.

1 Like
``````const humanDifference = Math.abs(targetNumber - humanGuess);
const computerDifference = Math.abs(targetNumber - computerGuess);

const getAbsoluteDistance = (humanGuess, computerGuess, targetNumber) => {
if (humanGuess, targetNumber) {
return humanDifference;
} else if (computerGuess, targetNumber) {
return computerDifference;
};
};
``````

Will this work as an function?

In the arguments we are using `humanGuess`, `computerGuess`, and `targetNumber`.

The conditional is used so that if, for example `humanGuess` and `targetNumber` are truthy (because the unused argument wouldn’t be used, so its falsy), it’ll declare a new variable called humanDifference which will work out the absolute value of `targetNumber` minus `humanGuess`.

It will do the same for the else-if conditional, where `computerGuess` and targetNumber are used, so they are truthy, and humanGuess is not used, so its falsy.

I’ve made changes in game.js to suit the change.

EDIT: realized that humanDifference and computerDifference are in block scope, which would probably stop it from being called, so I took it into global scope.

Perhaps, although it is not what the exercise is looking for. We want a simple, reusable utility function that takes only two arguments, a value and a target.

Eg.

``````const gad = (value, target) => Math.abs(value - target)
``````
2 Likes

Thanks! I’ll redo it later

1 Like

I’ve kind of given up on the extended questions. I reverted the file back to my original code. I think that I might come back to this some time later when I am more experienced in JavaScript.
The original code, which doesn’t include any extension codes for the extension questions, is correct, so I’m happy about that.
Thanks for your help, I appreciate it.

1 Like

Hello mtf

so by looking at your example to frank, i continue the code for No 8 ( Project extension and solution section ) exercise,
here is my code. I think it is working when i print with console.log for ‘functionality’ and ‘compareGuesses’ but ‘getAbsoluteDistance’ is wrong. It is printing ‘distance’. I don’t know why it supposed to be printing ‘2’ or some numbers ? i put console.log(getAbsoluteDistance(3,5)); I want to know ‘what is the absolute distance between userG and targetNum’ . What should i change please?
many thanks

const getAbsoluteDistance= (userG, targetNum) => {
const distance=Math.abs(userG- targetNum);
return ’ distance’;

}

const compareGuesses = (userG, targetNum) => {
if (userG<= targetNum) {
return true;}
else
{
false;}
}

const functionality= (userG) =>{
if (userG!==0 && userG!==9)
alert (‘the number is out of range’);
}

Your function returns `'distance'` because you told it to.

Compare this function to your `generateTarget` function. What did you return there?

Hint:

`distance` is not the same as `'distance'`. `distance` is the name of a variable that you assigned to a value:
`const distance=Math.abs(userG- targetNum);`
`'distance'` is a string literal.

Regarding:

Which numbers are in range? Is 5 in range, for example?

Lastly:

This function is called by code in another file named `game.js`. It is called with three arguments that represent the human guess, computer guess, and the target number - in that order. Your function only has two parameters. You are comparing the human guess to the computer’s guess, and returning `true` if the human guess is less than or equal to the computer’s guess.

the first one, i took out ’ ’ and it was coming right. I tried with console.log().

The second one , i don’t know how to put the numbers i want , example, I want to put 5 in range, how should I put it in the code? Also now I checked this functionality function and I think i put wrong. I want to write ‘if userG is not between 0 and 9, it should alert ’ the number is out of range’. I changed to ( if userG<0 || userG>9) alert ( ‘the number is out of range’). Then I checked console.log(functionality(10)) , now properller.cloud says ’ the number is out of range’ on top of my code but underneath, the code is showing error with red colour ’ alert is not defied’. So i don’t know what to do anymore .

The third one, compareGuesses, i have done with 3 parameters in first part of this lesson and now in No, 8 ( extension and solution question ), the exercise wants me to try with 2 parameters and i substitute 2 parameters exercise one in the place of 3 parameters ( i hid 3 parameters code) . Should i change totally the names of parameters ? please see the question 8 in Screensoht.
My code looks combining 1st part and 2nd part. Is it wrong ?

let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;
function generateTarget() {
let round = Math.floor(Math.random() *9);
return round;
}
const getAbsoluteDistance= (userG, targetNum) => {
distance= Math.abs(userG- targetNum);
return distance;
}
const compareGuesses = (userG, targetNum) => {
if (userG<= targetNum) {
return true;}
else
{
false;}
}
const functionality= (userG) =>{

if (userG < 0 ||userG >9 )
alert (‘the number is out of range’);
else {
}
}

//const compareGuesses = (human,computer,targetNum) => {

//const userG= Math.abs (human-targetNum);
//const computerG= Math.abs (computer-targetNum);

//return (userG <= computerG);
//my alternative
//if (userG <= computerG) {
//return true ;
//}
//else {
//return false;
//}

if (‘winner’ === ‘human’) {
humanScore +=1;}
else
{
computerScore +=1;
}
};
const advanceRound = () => {
currentRoundNumber += 1;
}

//console.log(humanScore);
//console.log(computerScore);
console.log(getAbsoluteDistance(3,5));
console.log (functionality(10));
console.log(compareGuesses(5,5));

Hello midlindner, from yesterday’s code above i sent , today i think again and amended. Now I think my code is right for No-8 extension question as it is not showing error but could you have a look if it is correct ? Is that what Q-8 asking ? Thanks

const getAbsoluteDistance= (userG, targetNum) => {
distance= Math.abs(userG- targetNum);
return distance;
}
const compareGuesses = (userG, targetNum) => {
if (userG<= targetNum) {
return true;}
else
{
false;}
}
const functionality= userG =>{

if (userG < 0 ||userG >9 ) {
return ‘the number is out of range’}
else {
return ‘it is in range’;
}
}
console.log(getAbsoluteDistance(3,5));
console.log (functionality(5));
console.log(compareGuesses(5,5));

printing :
2
it is in range
true

Hello, @arc2779423039.

Your logic for determining whether a number is in the range of (0-9) is now correct. The function, however, will not be called while a user plays the game. Implementing that functionality isn’t all that straightforward, if you want an out of range number to not crash the game. One idea would be to check the user’s guess by calling your `functionality` function from within your `compareGuesses` function. Rather that returning a message, you could `alert("Number out of range! You lose!")`, then return a bool to `compareGuesses` that indicates the computer automatically wins since the human chose a number out of range. Something like:

Code:
``````const compareGuesses = (userG, computerG, target) => {
if (validateGuess(userG)) {
return false; //remember this function returns true if the user wins or false if the computer wins
}
//then use the getAbsoluteDistance function to determine the winner if the human guess isn't out of range. Could be:
return getAbsoluteDistance(userG, target) <= getAbsoluteDistance(computerG, target); //will return true if the comparison is true or false otherwise
}

const validateGuess = num => {
if (num < 0 || num > 9) {
alert (`\${num} is out of range! You lose!`);
return true; // in this example, true means the number is out of range
}
return false;
}
``````

Your compareGuesses function still needs to have 3 parameters. You may have misunderstood the instructions for step #8. It asks you to write a separate `getAbsoluteDistance` function to avoid repeating code (abstraction). That function only has 2 parameters, the guess and the target number.

One last tip. The code you’ve pasted isn’t formatted. See this post to learn how to format code to retain it’s original formatting.

Keep up the good work! Happy coding!

1 Like

thank you very much midlindner. I will rewrite again this No-8 question. Before I started, I would like to know the following questions: Is this No-8 question is related to No1-7 or can I write all parameters not same as No 1-7? I just don’t understand still what is the purpose of doing No-8? is it for extra practice? For No-8 question, I don’t think it is important to relate with game.js? I should only test with console.log if my code is right or wrong right?

The 8th instruction is extra practice if you’d like to do it. The point is to improve upon the code you wrote for steps 1-7. In the end, it’s your project. You may do as you like. If you want to check off the steps, and move on without trying to integrate the suggestions in step number 8, that’s perfectly fine. My example above is one way to include the `alert()` suggestion while playing the game, but there are many other ways to accomplish the same thing.

1 Like

@midlindner , thank you again for your explanation. I would like to continue to No-8 and i was asking because i was not sure whether i should do the new parameters for getAbsoluteDistance function. IN this case , i will need to do. Your code is very good but i was doing in another way following the questions and I lost in one point. I called new parameters x and y for getAbsoluteDistance function.
When I put getAbsoluteDistance function in compareGueses function to compare but parameters ‘x and y’ are not related to human, computer, targetNum parameters. This is where I lost.
The question said I need to test getAbsoluteDistance function in compareGuesses. The last question of 'add functionality ’ ’ 0 or 9 ’ , I did it as per below.

Could you please let me know my understanding is still wrong?
thanks

const getAbsoluteDistance = (x,y) => {
return Math.abs(x-y)
}
const compareGuesses = (human,computer,targetNum) => {
return getAbsoluteDistance (x,y); ???
}

const validateGuess= num => {
if (num <0 || num > 9 {
alert (’\$ {num} is out of range!’);
return true;
}
return false;
}

We will want to call the helper function twice, once with human and targetNum, and once with computer and targetNum. Then we will compare the return values.

Hello mtf
So should it be as per below? I used the examples of midlinder which I understood well but I still don’t get the question. I think the problem is the question that makes me confused. It said ‘getAbsoluteDistance()function that takes two numbers and returns the distance’, so I put x and y as values but I don’t know how to use these 2 new arguments relate to compareGuesses function calling? I also read your advice like (value, target) , let say if i put these (value, target) instead of (x,y) then again how they are related with compareGuesses? I want to follow only what question wants for exercise but i don’t get it yet. There could be hundreds of ways outside but as I am a very beginner, i just want to know what the lessons want me to do and know only one way for the moment, then I will learn more in future.
Thanks

const getAbsoluteDistance (x,y) => Math.abs (x-y)

const compareGuesses = (user,computer,targetNum) => {
if (validateGuess(user)) {
return false;
}
return getAbsoluteDistance(user,targetNum)<=getAbsoluteDistance(computer,targetNum)
}

const validateGuess = num => {
if (num <0 || num >9) {
alert (’\${num} is out of range!’)
}
return false;
}