I’m currently making a temperature converter, and I want it to say “What you have entered is not a number, please try again” when the entered degree is not a number, and possible restart the program. But after all the ways I have tried I’m not really sure how to do that.
Here’s link to test: Js.do
<script>
//variables
var tempUnit = prompt("What unit do you want to convert from, 'F' or 'C'?").toUpperCase();
var tempNum = prompt("What is the number in degrees you want to convert?");
// if|else if|else
// f to c formula : [°C] = ([°F] − 32) × 5⁄9
if (tempUnit === "F"){
confirm( (tempNum-32) * (5 / 9) + "°C");
}
// C to F formula : [°F] = [°C] × 9⁄5 + 32
else if(tempUnit === "C"){
confirm(tempNum * (9 / 5) + 32 + "°F");
}
// If NaN(Not a Number)
else if(Number.isInteger === false){
confirm("What you have entered is not a number, please try again");
}
// input something else
else {
confirm("Please only enter what is allowed");
};
</script>
This is the base code:
<script>
var tempUnit = prompt("What unit do you want to convert from, 'F' or 'C'?").toUpperCase();
var tempNum = parseFloat(prompt("What is the number in degrees you want to convert?"));
while ((tempUnit !== 'F' && tempUnit !== 'C') || isNaN(tempNum)) {
alert("Entered values are invalid.");
tempUnit = prompt("What unit do you want to convert from, 'F' or 'C'?").toUpperCase();
tempNum = parseFloat(prompt("What is the number in degrees you want to convert?"));
}
if (tempUnit === "F") {
confirm((tempNum - 32) * (5 / 9) + "°C");
} else if (tempUnit === "C") {
confirm(tempNum * (9 / 5) + 32 + "°F");
}
</script>
Read about isNan
and parseFloat
and improve the conditions. You can always write your own function to test if the given input is numerical.
I have used parseFloat
to parse given input. This is important because result of the prompt is a string.
We don’t know how many times we will have to ask user for the correct input and this is why the while
loop is a good choice here
References:
Thank you so much!! The only thing I don’t completely understand is the parsefloat but other than that, it’s perfect!
You’re very welcome
Just a small example to show why using a parseFloat
is important when you want to prompt user for the number:
var x = prompt("Value of x:");
alert(x + " + 10 = " + (x + 10));
Let us assume that user inputs 2
. The output (text in the alert
) is:
2 + 10 = 210
Well, this obviously is not true The problem is that prompt
returns the string. So value of x
is "2"
. That is why some arithmetic operations will give you weird results.
To fix this problem you should use parseFloat
or parseInt
(if you ask only for the integer) functions to convert given string to the numerical value:
var x = parseFloat(prompt("Value of x:"));
alert(x + " + 10 = " + (x + 10));
And now the output is:
2 + 10 = 12
Do you understand now?
2 Likes
So, basically it changes the input from a string to a number.
Yes, exactly. It’s a good practice to always use parseInt
or parseFloat
when user inputs a numerical value.