0

i have a problem with conditions of typed text in prompt. I want to add conditions for prompt. Case A : If you type nothing in prompt - it will alert that you have inserted nothing. and opens prompt again until you type the number in. Case B: if you type text - it will alert that you haven't inserted number. and opens prompt again until you type the number in as in case A Case C: if you type number (What is essential, because it is a script which calculate the cubic equal) - it continues the script.

This script only finds out when you type nothing and press enter - It will alert you that you havent typed anything - BUT it will tell you even if you type number in - Which totally breaks the script.

And I also don't know how to define number in JS - How to find out when someone types in prompt for example "Word" - that is not a number, so again it suppose to tell you that you havent inserted number.

var a = prompt('Zadejte hodnotu a v kvadratické rovnici ax2 + bx + c = 0');
if (a == "") do {
alert('Nezadali jste žádné číslo, zadejte ho prosím');
a = prompt('Zadejte hodnotu a v kvadratické rovnici ax2 + bx + c = 0');
}
while (a = "something");
var b = prompt('Zadejte hodnotu b v kvadratické rovnici ax2 + bx + c = 0');
if (b == "") do {
alert('Nezadali jste žádné číslo, zadejte ho prosím');
b = prompt('Zadejte hodnotu b v kvadratické rovnici ax2 + bx + c = 0');
}
while (b = "something");
var c = prompt('Zadejte hodnotu c v kvadratické rovnici ax2 + bx + c = 0');
if (c == "") do {
alert('Nezadali jste žádné číslo, zadejte ho prosím');
c = prompt('Zadejte hodnotu c v kvadratické rovnici ax2 + bx + c = 0');
}
while (c = "Something");

Thanks a lot!

JustTop
  • 11
  • 2

2 Answers2

0

You can simplify it with a while loop to detect that the value is isNAN (is not a number) or is empty.

val = "";
while (isNaN(val) || val == "") {
  val = prompt("Please enter in a value");
}

if(val != null){
   console.log(val);
}
imvain2
  • 15,480
  • 1
  • 16
  • 21
0

Thanks, your code works just fine. But if i want to tell the user using alert command for each value a = "" and a = isNaN, what i suppose to do

this code does not work

 a = ""; 
 a = prompt('Zadejte hodnotu A v kvadratické rovnici ax2 + bx + c = 0');
 while (isNaN(a)) {
 alert('Vámi zadaná hodnota není číslo!');
 a = prompt('Zadejte hodnotu A v kvadratické rovnici ax2 + bx + c = 0');
 }
 while ( a == "" ){
 alert('Nezadal jste žádné číslo.');
 a = prompt('Zadejte hodnotu A v kvadratické rovnici ax2 + bx + c = 0');
 }
JustTop
  • 11
  • 2
  • Instead of an alert for each, you could keep it simple and the message in the prompt say something like: "Please enter in a valid number" and what ever else your message is saying (I don't know the language). – imvain2 Oct 08 '20 at 19:40