Number Guesser game

Hi! I am having difficulty with an if else statement in the game Number Guesser… I have looked through previous discussions but could not find help…

https://www.codecademy.com/practice/projects/number-guesser-independent-practice

See code below, I do not understand why the if else statement always gives false…

Can you help?

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


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

//function compareGuesses(HumanGuess, computerGuess, targetNumber) 

let humanGuess = 5;
let computerGuess = Math.floor(Math.random() * 10);
let targetNumber = generateTarget();



const compareGuesses = (humanGuess, computerGuess, targetNumber) => {
    if ((Math.abs(humanGuess - targetNumber)) < (Math.abs(computerGuess - targetNumber))) {
        return true;
    } else {
        return false;
    }
}



console.log(humanGuess)
console.log(computerGuess)
console.log(targetNumber)

console.log('human' + (Math.abs(humanGuess - targetNumber)))
console.log('computer' + (Math.abs(computerGuess - targetNumber)))

console.log(compareGuesses())

Well, you are calling compareGuesses function without any inputs…

1 Like

I tried with this statement and then it works! Whats the difference (except for adding the = so that human also wins when it is a tie) ?

const compareGuesses = ((Math.abs(humanGuess - targetNumber)) <= (Math.abs(computerGuess - targetNumber)))
    ? true : false


so how would i test my code? using default values like this?

const compareGuesses = (humanGuess = 5, computerGuess = 2, targetNumber= 4) => {
    if ((Math.abs(humanGuess - targetNumber)) < (Math.abs(computerGuess - targetNumber))) {
        return true;
    } else {
        return false;
    }
}

Yes you could but I have the feeling you don’t quite understand or may have forgotten how input parameters work.

const compareGuesses = (humanGuess, computerGuess, targetNumber) => {

The above code means that the function compareGuesses expects three input parameters, humanGuess, compyterGuess and targetNumber. The function would then process these parameters inside the function.

However these input parameters are not (global)variables but you can pass variables as parameters upon calling the function.

To call a function that expects input parameters you pass variables or values as input for those parameters. Lets take a simple function as example.

const whatIsForDinner = food => {  
    console.log('Today I eat ' + food)
}

The above mention function takes one input parameter and logs it in a string. Now to call this function you can pass any value or variable. For example:

whatIsForDinner('apple') // results in 'Today I eat apple'

or

let a = 'banana' // variable a with value of 'banana'

whatsIsForDinner(a) // results in 'Today I eat banana'

// If you don't pass any value or variable the following happens

whatIsForDinner() // results in 'Today I eat undefined'

So what do these examples tell you when comparing to your code?

1 Like

This is no function, you are simply storing the value of the outcome of your boolean operations. :wink:

If it works, it works, but the exercise expects you to write a function.

thanks for the quick replies! I understand it, i am still trying to the the hange of it…

So answering your question: because you added an input voor value ‘a’ and gave the function-parameter value a. it returns banana when invoking the function.

Because the last function has no parameter while invoking, it returns undefined.

2 Likes

oke thanks! all use a function. Is below a function?

const compareGuesses = () => {}

or should i be using:

function compareGuesses () {} ?

1 Like

So answering your question: because you added an input voor value ‘a’ and gave the function-parameter value a. it returns banana when invoking the function.

Because the last function has no parameter while invoking, it returns undefined.

1/2 yes :wink:

The last one, the function expects a parameter but you are not passing anything to it so it gets an undefined as value.

Now coming back to your code.

const compareGuesses = (humanGuess, computerGuess, targetNumber) => {
    if ((Math.abs(humanGuess - targetNumber)) < (Math.abs(computerGuess - targetNumber))) {
        return true;
    } else {
        return false;
    }
}

console.log(compareGuesses()) // You are not passing values for the parameters

What happens is that it will get three undefineds as values for the parameters. Causing the if statement to return a false :wink:

This is the arrow notation for function compareGuesses () = {} so it doesn’t really matter which one you use. I prefer arrow notations myself.

thanks!

Why does my if else statement return undefned when i would expect false?

let humanGuess = 5;
let computerGuess = Math.floor(Math.random() * 10);
let targetNumber = generateTarget();

const compareGuesses = (humanGuess, computerGuess, targetNumber) => {
    if ((Math.abs(humanGuess - targetNumber)) <= (Math.abs(computerGuess - targetNumber))) {
        return true;
    }
    else {
        false;
    }
}

console.log(humanGuess)
console.log(computerGuess)
console.log(targetNumber)

console.log('human' + (Math.abs(humanGuess - targetNumber)))
console.log('computer' + (Math.abs(computerGuess - targetNumber)))

console.log(compareGuesses(humanGuess, computerGuess, targetNumber))

Well actually you are now not returning anything :stuck_out_tongue: . No worries we’ll get to the bottom of this.

else {
    false;
}

It reaches this section of the code but it results in logging undefined. Because you are not returning the false.

It should of course be:

else {
   return false;
}

Welcome to the world of debugging :wink:
Happy coding!

ofcourse!! oepsiee…

Thanks alot

1 Like

Anytime :wink:

Where are you from? A native English speaker would say “oopsiee”

the Netherlands… You are from Enschede right?

1 Like

Hi! I am back…

Why does the code below not add anything to the humanscore or computerScore? I feel like i am missing something…

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


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

//function compareGuesses(HumanGuess, computerGuess, targetNumber) 

let humanGuess = 5;
let computerGuess = Math.floor(Math.random() * 10);
let targetNumber = generateTarget();

const compareGuesses = (humanGuess, computerGuess, targetNumber) => {
    if ((Math.abs(humanGuess - targetNumber)) <= (Math.abs(computerGuess - targetNumber))) {
        return true;
    }
    else {
        return false;
    }
}

const winner = (compareGuesses(humanGuess, computerGuess, targetNumber) === true) ? 'Human' : 'Computer'

console.log(winner)

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

console.log(humanScore)
console.log(computerScore)
console.log(currentRoundNumber)

Hey Emma,

Defining a function does not run it, you also need to call it.
Have you called the updateScore function?

Also you want to add a failsafe feature to your updateScore function. I suggest adding an else statement that logs an error.

const updateScore = () => {
    if (winner = 'human') {
        humanScore = humanScore + 1;
    } else if (winner = 'computer') {
        computerScore = computerScore + 1;
    } else {
        console.log('Error: something might be wrong')    
    }
}

can I call it by adding this?

updateScore() or console.log(updateScore())

I tried both, with no difference

Yes you call it by adding either one of them. Did you add the else statement to your updateScore function aswel?