0

Here is my code:

    let guessTheNumber = () => {
        let randomNumber = Math.round((Math.random()) * 10); //generating a random number from 1 to 10
        console.log(randomNumber); //added this just to see what number was generated
        let question = +prompt('Please, try to guess the number from 1 to 10!'); // by using unary plus I want prompt to return a number, NOT a string
        if (question === randomNumber) {
            alert('Wow, you are quite lucky. Nice job!'); //this one works 
        }
        else if (question !== randomNumber) {
            alert('Nope'); //this one is also easy to check
        }
        else if (question === "") {
            alert('You did not enter anything!'); 
        }
        else {
            alert('Why did you cancel?');
        }
    }
    guessTheNumber();

I can successfully check the question when it equals to randomNumber variable or not. But when I try to alert something, if there is an empty string (clicking "OK" without any input) or null (clicking "Cancel"), the Program fails.

Asad ali
  • 147
  • 2
  • 11
  • 1
    With the + before prompt you are casting to number, then the === '' fails because the third equal operator fails because it is not a string – quirimmo Jan 15 '20 at 16:16
  • and it fails because `question` is NaN, your conditions are also... wrong (or at least wrong way round), either `question` equals or it doesn't, the other two conditions will never be hit. – Adrian Jan 15 '20 at 16:17

2 Answers2

1

The (+) before the prompt will convert the response to a Number so if user canceled or leave the prompt empty it will always return 0

So if you need to check for cancel you need to remove the (+) then the prompt will return a string or null and so you'll need some extra logic.

let guessTheNumber = () => {
  // using 'Math.ceil' as @symlink mentioned
  let randomNumber = Math.ceil((Math.random()) * 10);
  console.log(randomNumber);
  let question = prompt('Please, try to guess the number from 1 to 10!');

  // check for cancel 'null'
  if (question == null) {
    alert('Why did you cancel?');
    // you need to exit so it won't prompt again
    return
  }
  // parseInt() function parses a string argument and returns an integer
  else if (parseInt(question, 10) === randomNumber) {
    alert('Wow, you are quite lucky. Nice job!');
  }
  // empty value
  else if (question === '') {
    alert('You did not enter anything!');
    // run again
    guessTheNumber();
  }
  // not a number
  else if (isNaN(question)) {
    alert('Please enter a number');
    // run again
    guessTheNumber();
  }
  // wrong answer
  else {
    alert('Nope!')
    // run again
    guessTheNumber();
  }
}
guessTheNumber();
awran5
  • 4,333
  • 2
  • 15
  • 32
0

First, you want to Match.ceil() your random number, not Math.round(). Then for incorrect data check if the guess is null, not a number, or an empty string with:

!guess || isNaN(guess) || guess.trim() === ""

let guessTheNumber = () => {
    let ranNum = Math.ceil((Math.random()) * 10)
    
    let guess = prompt("Please, try to guess the number from 1 to 10!")
    
    if (parseInt(guess, 10) === ranNum) {
        alert("Wow, you are quite lucky. Nice job!")
    }else if(!guess || isNaN(guess) || guess.trim() === ""){
        alert("You didn't enter a number")
    }else{
        alert("Nope")
    }    
    console.log(guess, ranNum)
}
guessTheNumber();
symlink
  • 11,984
  • 7
  • 29
  • 50
  • You forget to check for cancels and so the `trim` will be called by `null` and threw error. – awran5 Jan 15 '20 at 18:46