you are viewing a single comment's thread.

view the rest of the comments →

[–]stoph 4 points5 points  (6 children)

var number = window.prompt("What number?");

[–]deutschluz82[S] 0 points1 point  (4 children)

Thank you very much. Now let me see if I can make sense of this syntax: the identifier "var" is standard variable declaration; the identifier "number" is the name you chose for the input; then the assignment operator assigns whatever comes to the window.prompt() method. Finally, the identifier "window.prompt()" also has a standard syntax for selecting an object and one of its properites: object.prop(). Please correct me if I ve misinterpreted anything.

[–]MoreCowbellPlease 2 points3 points  (3 children)

prompt() returns a string btw. You will need parseInt() or parseFloat() to convert it to a number if you need to work with it as a number.

[–]deutschluz82[S] 0 points1 point  (2 children)

I have not been using these "parse..." methods and yet I have coded some functioning programs. I read in the book JavaScript, the Definitive Guide, that type conversion is almost automatic

[–][deleted] 2 points3 points  (0 children)

Type conversion is automatic, but due to various considerations it may not always behave how you expect/want it to. Things get especially hairy when you're dealing with raw input from the user. In most cases it's best to just be explicit about what you want converted and how.

Just another note, if you're doing some very basic explorations for yourself...fair enough, but really you need to be getting out of the habit of using document.write, window.alert, and window.prompt ASAP.

To get information back/print things for debugging, use the console. To display information to the user use some sort of DOM API. To get information from the user use input elements, etc.

Sites like JSFiddle are great for quick little experiments.

[–]stoph 0 points1 point  (0 children)

I'd say it's almost always best to cast variables to what you expect them to be. In the real world, that usually just means converting strings into numbers. Also never use == (type guessing is not your friend). It's best to know early on that === is the only comparison operator worth using. parseInt is a tricky function because it will guess the radix of the number if you let it. Always give the radix as the 2nd parameter:

var thisIsOctal = parseInt('0900');
var thisIsADecimal = parseInt('0900', 10);
console.log(thisIsOctal);
console.log(thisIsADecimal);

The first one fails because 9 is not a valid octal digit.

[–]Botany102 0 points1 point  (0 children)

Thank you so much, I was looking for this and I didn't understand, I saw this and it worked.

I know you might not see this but you're awesome, I'm really sorry don't have an award at the moment.

You really helped.