2

Trying to have a user input a number and if they type in a string I would like to prompt them to enter a number. Seems like I got that part right but how can I get the second prompt to keep popping up until the user enters a an actual number. As of now, once the user enters a string again nothing runs after that. Would appreciate any kind of suggestions.

Here is the code:

function enterNumber(n) {

    n = parseInt(prompt("Please enter a number: "));

    if (isNaN(n)) {
        n = parseInt(prompt("You did not enter a number. Please enter a number: "));

            for(var i = 1; i <= n; i++) {

            if (i % 15 === 0) {
                document.write("Fizz Buzz" + "<br>");
                continue;
            }
            else if (i % 3 === 0){
                document.write("Fizz" + "<br>");
                continue;
            } else if (i % 5 === 0) {
                document.write("Buzz" + "<br>");
                continue;
            }
            document.write(i + "<br>");
        }
    }

};
enterNumber();
1
  • Give them a input field, and only run the function which has the loop if the input is a number. Commented Sep 24, 2015 at 5:04

2 Answers 2

2

Use while loop until the entered is number.

function enterNumber(n) {
  while (isNaN(parseInt(n))) {
    n = parseInt(prompt("Please enter a number: "));
  }

  for (var i = 1; i <= n; i++) {

    if (i % 15 === 0) {
      document.write("Fizz Buzz" + "<br>");
      continue;
    } else if (i % 3 === 0) {
      document.write("Fizz" + "<br>");
      continue;
    } else if (i % 5 === 0) {
      document.write("Buzz" + "<br>");
      continue;
    }
    document.write(i + "<br>");
  }

};

enterNumber();

You can also shorten you code using nested Ternary operators as follow.

function enterNumber(n) {
  while (isNaN(parseInt(n))) {
    n = parseInt(prompt("Please enter a number: "));
  }

  for (var i = 1; i <= n; i++) {
    var title = i % 15 === 0 ? 'Fizz Buzz' : i % 3 === 0 ? 'Fizz' : i % 5 === 0 ? 'Buzz' : i;
    document.write(title + "<br>");
  }
};

enterNumber();

Sign up to request clarification or add additional context in comments.

1 Comment

Oh thats even better I am gonna read up on that...looks like it will make life a lot easier
1

Try like this

function enterNumber(n) {


  while (isNaN(n))
    n = parseInt(prompt("You did not enter a number. Please enter a number: "));


  for (var i = 1; i <= n; i++) {

    if (i % 15 === 0) {
      document.write("Fizz Buzz" + "<br>");
      continue;
    } else if (i % 3 === 0) {
      document.write("Fizz" + "<br>");
      continue;
    } else if (i % 5 === 0) {
      document.write("Buzz" + "<br>");
      continue;
    }
    document.write(i + "<br>");
  }


};
enterNumber();

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.