3

My exercise is to force a user to type a number and check that it is less than 100. I think I've done this well but there is another case I don't know how to do. If the user does not type any number in the space, the program should show something like "you must type a number". How should I write the code?

var number=prompt('enter a number');
if (number<100){
    newnumber=100-number;
    document.write(number+'is less than 100 by'+ newnumber);
}else if(number>100){
    document.write('type again');
}
5
  • i think using prompt you cannot do this. Commented Nov 6, 2016 at 18:11
  • 4
    @Mahi Of course you can... Commented Nov 6, 2016 at 18:15
  • @Brad how can you disable prompt for returning empty string value ? Commented Nov 6, 2016 at 18:17
  • @Mahi You re-show the prompt. Commented Nov 6, 2016 at 18:18
  • @Brad that's a good idea :D Commented Nov 6, 2016 at 18:23

3 Answers 3

2

You can determine if the users input is a valid number by using the isNaN function. I have also validated the blank character for you, as shown below.

var isValid = !isNaN(number) && number !== "";

Full snippet:

var number = prompt('enter a number');
number = number.replace(/\s/g, ""); 
var isValid = !isNaN(number) && number !== "";

if (isValid) {
  if (number<100) {
     newnumber=100-number;
     document.write(number+'is less than 100 by'+ newnumber);
  } else if(number>100) {
      document.write('type again');
  }
} else {
   document.write("Looks like you didn't enter a valid number");
}

https://jsfiddle.net/ezgn5cv5/

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

4 Comments

when i click ok after typing nothing it shouldn't go to if condition
@Darren Davies i think you should remove spaces to avoid extra spaces also empty inputs "" will be 0
@MamdouhFreelancer - then you can add another check && number !== "" - please see my update.
@DarrenDavies try to input multi space it will be 0 and outputs is less than 100 by100
1
var number = null;
while (number !== 0 && !number || number >= 100) {
  number = parseInt(prompt('Enter a number, less than 100'));
}

document.write(
  number + 
  ' is less than 100 by ' + 
  (100 - number)
);

This puts us in a loop for whether or not the number is a valid integer (I assumed that's what you wanted, but you could change this to float or something else), and under 100. Only when the user's input is valid will it go to the line to output.

The second condition for the while loop is !number. This basically tests for falsy conditions, such as NaN or null. If parseInt() can't figure out what the user typed in for a number, it will return NaN. And, of course, we initialized the number variable to null.

The first condition for while is number !== 0 is actually required because of the second condition which tests for falsy. 0 is falsy, but 0 is a valid number less than 100, so we need to make sure that we let 0 be valid. Conditionals like these short circuit. That means that they are processed from left to right, and any condition failing the test will immediately bypass the conditional block of code below. If number is 0, we know that the whole condition is false and we can move on.

The third condition simply ensures we're under 100 by re-prompting if we're not.

Also, I should note that document.write() has some issues. It's better to select an element on the page and set its text.

Comments

1
  • Remove all spaces .replace(/\s/g, "").
  • Detect if user input a number using parseFloat() if you want to allow user to input decimal numbers like 5.254 or only integers using parseInt() like 5.
  • Then detect if number > 100 or number < 100.

See this example:

var number = prompt('enter a number');
    number = number.replace(/\s/g, ""); //remove all spaces

if (number != "") { // if not empty
  if (parseFloat(number) == number) { // if decimal/integer number
    if (number < 100) {
      newnumber = 100 - number;
      document.write(number + ' is less than 100 by ' + newnumber);
    } else if (number > 100) {
      //number = prompt('enter a number');
      document.write('type again');
    }
  } else {
      //number = prompt('enter a number');
    document.write('you must type a number');
  }
} else {  // if empty input
      //number = prompt('enter a number');
  document.write('shouldn\'t be empty');
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.