Reputation: 13
I'm self teaching myself JavaScript from videos I've dug up from around the Internet. Most tend to gloss over things quickly and leave tons of questions. So I test things within Firefox 21 using Firebug 2.0.2 to see how they behave. That's the only purpose of the following code.
Console.log output
>>> var n=parseInt("34","22","18",10);console.log(n);
70
My initial thoughts was that it parsed each of the supplied strings and then returned the sum. Then I did the actual math and realized the sum should have came out to be 74 if that were the case.
Even more interesting, and what actually prompted me to do the math on my own instead of just trusting that to be the sum without thinking. When I reduced the value of one of the numeric strings (22 to 21 in my experience anyways) returned 67 instead of the expected 69.
in:10>>> var n=parseInt("34","21","18",10);console.log(n);
67
What is actually happening here? Is parseInt just not designed to accommodate multiple numeric strings?
Upvotes: 1
Views: 271
Reputation: 4921
No parseInt does not take multiple strings, it take one string and a optional radix (i.e. what base the number is in)
Please refer to the MDN reference here
Whats happening in your last example
parseInt("34","21","18",10)
is that you are telling javascript to parse 34 in base 21 (the other parameters 18 and 10 are ignored)
34 in base 21 is 67 (i.e. 3 x 21 + 4)
EDIT: and 34 in base 22 is 70 (3 * 22 + 4). See this answer for an informative approach to convert bases: https://answers.yahoo.com/question/index?qid=20090508092544AAxAokR
If you want to add up numbers, parse them individually first, then add them
i.e.
var n1 = parseInt("34",10);
var n2 = parseInt("22",10);
var n3 = parseInt("18",10);
var result = n1 + n2 + n3;
Also if you are positive that the strings only ever contain numbers (i.e. "22", not something like "22 widgets", because parseInt will parse everything up to the first non numeric character so parseInt("22 widgets") will return 22), then you can cheat a bit, and just put a + at the start
i.e.
var n1 = +"34";
var n2 = +"22";
var n3 = +"18";
var result = n1 + n2 + n3;
same outcome
Upvotes: 4
Reputation: 1240
parseInt
takes 2 arguements:
1. Numuric String
2. Radix - Number System (2 to 36)
parseInt("50",10);// Decimal equivalant 50
parseInt("10",2); // Binary equivalant 2
parseInt("50",8); // Octal equivalant 40
parseInt("50",16); // Hexadecimal Equivalant 80
See in the above example parseInt("10",2);
, (10)Binary === (2)Decimal
Refer: MDN DOCS
Upvotes: 0
Reputation: 13621
parseInt
takes one required argument and one optional argument, and ignores everything else. The optional (second) argument is the radix, which tells the function which numerical base to use when converting the string.
From the MDN docs on parseInt
:
The following examples all return 15:
parseInt(" 0xF", 16)
parseInt(" F", 16)
parseInt("17", 8)
parseInt(021, 8)
parseInt("015", 10)
parseInt(15.99, 10)
parseInt("1111", 2)
parseInt("12", 13)
So, what's happening in your example is that your first function call is interpreting the characters "34" in base 22 and returning 70, and the second function call is interpreting the characters "34" in base 21 and returning 67.
Upvotes: 1