dianzishangwu Posted September 21, 2006 Share Posted September 21, 2006 check out this example:<html><body><script language="JavaScript" type="text/javascript">var degFahren=prompt("enter the degrees in Fahrenheit",50);var degCent;degCent=5/9*(degFahren-32);alert(degCent);</script></body></html>my question is why not use Number(prompt("enter the degrees in Fahrenheit",50));here cauze I think things within prompt are always strings..then we want to use the number so need to use number method to convert the string to actual number,this is an example from a text book..anyone has this kind of experience to share? when you use prompt and you may want to actual numbers, so do you often use Number method to transfer?cheers! Link to comment Share on other sites More sharing options...
jesh Posted September 21, 2006 Share Posted September 21, 2006 Javascript doesn't truly have variable types like string, int, byte, etc. All variables are of type var. So you could, technically, perform: var test = 5 / "wtf"; The value assigned to test would be "NaN", so this might help your script: <script language="JavaScript" type="text/javascript">var degFahren=prompt("enter the degrees in Fahrenheit",50);var degCent;degCent=5/9*(degFahren-32);if(isNaN(degCent)){ alert("You didn't enter a number!");}else{ alert(degCent);}</script> Link to comment Share on other sites More sharing options...
Webworldx Posted September 21, 2006 Share Posted September 21, 2006 or a while loop and keep bringing up the prompt box until they enter a number or exit out :)I've never used Number() to be honest, although not sure why - I just prefer the Math.floor() and parseInt() functions Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now