1

I want to ask the user for a number, while his answer is not a number the question will keep on looping (as an alert box).

I tried to do that with while loop and isNaN, but i'm doing something wrong.

Here is my Js:

var correct = false;

do {
    var question = prompt("guess the number?");
    if (question !== isNaN) {
        document.write("your number is " + question);
        correct = true;
        break;
   }

} while(! correct);
3
  • 4
    isNaN is a function! Commented Apr 11, 2016 at 15:20
  • oh, you are right. I changed it to isNaN() but still it doesn't work. Commented Apr 11, 2016 at 15:23
  • why did I get -1 on this? can somebody explain? Commented Apr 11, 2016 at 15:35

6 Answers 6

2

A couple of mistakes

  • isNaN() is a function: isNaN(NaN) returns true while isNaN(1) or isNaN("1") returns false
  • prompt() always returns a string (never NaN)

You can convert the result from prompt() to a number using the unary + operator, then check if it is NaN;

var question = prompt("guess the number?");
if (!isNaN(+question)) {
  ...

That said, isNaN() is a bad fit if you're simply trying to see if a string contains only numbers, since whitespace ("" or " " or "\n\t") will all convert to 0and give you false positives.

A simple regex will do the right thing;

var question = prompt("guess the number?");
var containsOnlyDigits = /^[0-9]+$/; // one or more of digits 0 to 9
if (containsOnlyDigits.test(question)) {
  ...
Sign up to request clarification or add additional context in comments.

Comments

2

I will give you some tips with the following sample:

<html>
 <head>
    <title></title>
    <script>

    var answer = "";

    do {
        answer = prompt("guess the number?");
    } while(!isNumber(answer));

    document.write("your number is " + answer);

    function isNumber(value) {
      var numberPattern = /^[0-9]+$/; // one or more of digits 0 to 9
      return numberPattern.test(value);
    }

    </script>
 </head>
 <body>

</body>

Example here: https://plnkr.co/edit/ziysG36if9OTZahHfSbO

  • It is not necessary to create an additional variable called correct to check if the condition is true or false because you have the condition clear, isNan(answer), so the while should use that condition "while (isNan(answer))".

  • When you write code you should write as clean as possible and if you are saving the result of prompt it is more clear to name the variable "answer" because you are saving the answer and not the question, which is a method call.

9 Comments

thanks! can you explain why did you declare the var answer outside the loop and called it again inside? instead of declaring the variable inside the loop? thx
@xec The loop is expected to run once if the answer is a number as he wrote in his question "while his answer is not a number the question will keep on looping (as an alert box).".
@CodeChunk edited, it's unnecessary to declare it outside of it's scope, which is actually the while loop
can you explain it more detailed about declaring a variable outside of the loop or inside?
@CodeChunk For this example doesn't make much difference, in other languages you can't access variables out of their strict scope, which means you should not be able to access "answer" out of the while if it is delcared inside, but is not applicable now.
|
1

isNan is a function - you need to call it by putting parentheses after it, and then putting your arguments inside those parentheses.

The isNaN function returns true if the argument isn't a number, and false if it is. Since you want to check if question is a number, we can then invert the boolean output using the ! prefix, which will cause the if statement's body to trigger if question is a number.

var correct = false;

do {
  var question = prompt("guess the number?");
  if (!isNaN(question)) {
    document.write("your number is " + question);
    correct = true;
    break;
  }
} while (!correct);

Comments

1

isNaN is a function with some special rules. You should first try to parse the input to a number and then use isNaN on that result. Number returns NaN, if the conversion from a string to number failed.

var input1 = "1234";
var input2 = "abcd";

alert(isNaN(Number(input1)));
alert(isNaN(Number(input2)));

Comments

1

As mentioned above - isNan is a function which gets a parameter as its input.

You don't have to convert the prompt to a number before the test in isNan as you can see in the documentation it can accept strings as well.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/isNaN#Examples

The function will do the conversion internally behind the scenes:

You could think of isNaN as:

isNaN = function(value) {
   Number.isNaN(Number(value));
}

Here is a code snippet which will work for you:

var correct = false;

do {
    var answer = prompt("guess the number?");

    // The isNaN function gets an number argument
    // You also need to check if there is value.
    // If the user click no ESC you will get null
    if (answer && !isNaN(answer)) {

        // You cant write to the document once its loaded.
        // it will clear any previous content in the page
        document.write("your number is " + answer);
        correct = true;
        break;
    }

} while (!correct);

Comments

0

Here is a similar solution:

for (let numTrue = false; !numTrue;) {
	var question = prompt("Guess the Number!", "Number");
	numTrue = !isNaN(question);
}
document.write("Your number is " + question);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.