samedi 28 avril 2018

If user's answer is NaN, alert NaN and ask again for a number

I'm writing a function that asks a user for a number and returns the sqrt of that number. If they don't provide a number I need to alert(NaN) and prompt for a number again. Not sure what I'm doing wrong here, it is returning the sqrt but I can't get the NaN part to work.

function sqrtUserNum() {
  let userNum = parseFloat(prompt("Give me a number and I'll tell you it's square-root."));
  if (isNaN(userNum)) {
    alert(NaN)
    prompt(userNum)
  }
  if (!isNaN(userNum)); {
    alert(Math.sqrt(userNum))
  }
}
sqrtUserNum()

Aucun commentaire:

Enregistrer un commentaire