Jump to content

string in prompt?


dianzishangwu

Recommended Posts

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...