Number Guesser - can't make the Alert Message work

Hi everybody!

So I’m having a bit of a challenge here finishing the Number Guesser project. Basically I’m stuck on the last extra task where you have to add a functionality that checks whether the user guess is between 0 and 9.
Everything is working, but is just the message that’s not popping up.
I’ve searched on the forum and even with the solutions there I can’t understand why my code isn’t working…

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

// Write your code below:
//task 3//
const generateTarget = () => {
 return randomNumber=(Math.floor(Math.random()*10));
}
//extra task//

const getAbsoluteDistance = (a,b) =>{
return Math.abs(a - b);
}
//task 4//
const compareGuesses = (human, computer, target) => {
  const humanG = getAbsoluteDistance(target, human);
  const computerG = getAbsoluteDistance(target, computer);

  return humanG <= computerG ? true : false;

}
//Create Alert//

const check = () => {
  if (human < 0 || human > 9){
    window.alert ('Please, keep in range');
  }
}
//task 5//
const updateScore = (winner) => {
  if (winner = 'human') {
    return humanScore += 1;
  }else{
    return computerScore+=1;
  }
}
//task 6//
const advanceRound = () => {
 currentRoundNumber+=1;
}

I.ve tried using just the alert() command, instead of the window.alert, but nothing happens…

Hi Rui,
where do you call check()?

Hi Mirja!

So, I didn’t call it though. I’ve just created this function with the conditional in it

//Create Alert//

const check = () => {
  if (human < 0 || human > 9){
    window.alert ('Please, keep in range');
  }
}

Should I call the function in the code as well?

If you don’t call your function, it’s not going to run. You need to think where you want to call it. That depends on when you want the alert to show up.

1 Like

It makes sense. So I called the function in the compareGuesses() function, now. But it’s still not working… could it be that the alert() command is not supported in the codecademy text editor? Or maybe I’m being very thick, sorry

//task 4//
const compareGuesses = (human, computer, target) => {
  const humanG = getAbsoluteDistance(target, human);
  const computerG = getAbsoluteDistance(target, computer);

  return humanG <= computerG ? true : false;

check(); 
}

//Create Alert//

const check = (human) => {
  if (human < 0 || human > 9){
    return alert ('Please, keep in range');
  }
} 

Everything after the return statement is ignored. You need to call your function before the return.
Also, your check function expects a parameter but you don’t pass one when you call it.

1 Like

Oh wow, it finally worked. Thank you so much for your help!

1 Like