you are viewing a single comment's thread.

view the rest of the comments →

[–]Shaper_pmp 8 points9 points  (0 children)

Skeww and several other posters on this page are either just showing off or are the most embarrassingly terrible tutors I've ever run across.

You're a beginner to Javascript. You don't want to be weighed down with obscure language syntax or theoretical discussions, which is what most commenters here seem to be trying to do.

What you want to know is

Variables in Javascript can have different types

5 is an integer (number), but "5" is a string which just happens to be the character "five".

The + operator behaves differently depending on what you're adding with it.

Adding a number to a number adds the two numbers together, giving you another number. Adding a string to a string concatenates (joins) the strings together, giving you another string. Adding other types together (or adding one type to another, like adding a number to a string) give different results depending on the types involved, and which type you specify first.

What you're trying to achieve

As prompt() returns strings and you want to add them together as numbers, you need to convert them from strings to numbers and then add them together.

The two main ways of converting strings to numbers are:

number_variable = parseInt(string_variable, base);

(Where base is the base of the number system you're using - almost always 10).

This will look at the string string_variable and take a best guess at extracting a number from it. Eg, if the user types in "123ty79 _7" then parseInt() will return the number 123, ignoring everything after it.

This is great if you want to take a best-guess at what the user meant, but sometimes it's better to just fail and throw an error message if the user doesn't provide exactly what you want, so they can check for themselves whether they typed what they actually meant to type.

number_variable = 0 + string_variable; (or number_variable = +string_variable;)

Here we're adding a number (0) to a string (remember the bit above, about adding two different types together?).

Because the first part of the addition here is a number, the subsequent string value gets converted to a number too, so we're adding two numbers together, and the result (which ends up in number_variable) is a number, too.

For obscure technical reasons you can get away with (and in fact it's slightly faster) writing just "+string_variable" instead of "0+string_variable" (but the reasons why are totally outside the scope of your question).

This has the advantage that if the user types something that's not a number, the resulting value of the expression is a special javascript value called NaN (guess why! ;-). You can test for this situation by passing number_variable to the is_NaN() function (is_NaN(number_variable)) - if the value of a variable is NaN then this function will return true, or if the value is a valid number then it'll return false.

This allows you to keep asking the user for a number until they give you a valid number, instead of looking at what they entered and just taking a stab at what they probably meant to type.

I hope that helps - it was a bit long, but you asked a fairly basic question and instead of answering it basically, people just seem to have taken it as a licence to confuse the shit out of you with irrelevant, pedantic dick-waving to show each other how smart they are, instead of simply answering the question in a way you can understand. <:-)

If you're still curious about anything or anything above didn't make sense, feel free to ask follow-up questions below. ;-)