views:

1801

answers:

6

How can I do the following in JavaScript?

Step (1) Concatenate "1", "2", "3" into "123"

Step (2) Convert "123" into 123

Step (3) Add 123 + 100 = 223

Step (4) Covert 223 into "223"

+2  A: 

You can do it like this:

// step 1 
var one = "1" + "2" + "3"; // string value "123"

// step 2
var two = parseInt(one); // inegral value 123

// step 3
var three = 123 + 100; // integral value 223

// step 4
var four = three.toString(); // string value "223"
Andrew Hare
Best practice would be to add the radix parameter to the parseInt() function. so, parseInt(one, 10) assures that whatever you throw at it won't get misconverted.
artlung
+5  A: 

Step (1) Concatenate "1", "2", "3" into "123"

 "1" + "2" + "3"

Step (2) Convert "123" into 123

 parseInt("123", 10)

Step (3) Add 123 + 100 = 223

 123 + 100

Step (4) Covert 223 into "223"

 223.toString()

Put it all together:

 (parseInt("1" + "2" + "3", 10) + 100).toString()
Patrick McElhaney
+7  A: 

You want to become familiar with parseInt():

And toString():

And useful in your toolkit will be to look at a variable to find out what type it is - typeof:

<script type="text/javascript">
    /**
     * print out the value and the type of the variable passed in
     */

    function printWithType(val) {
     document.write('<pre>');
     document.write(val);
     document.write(' ');
     document.writeln(typeof val);
     document.write('</pre>');
    }

    var a = "1", b = "2", c = "3", result;

    // Step (1) Concatenate "1", "2", "3" into "123"
    // - concatenation operator is just "+", as long
    //   as all the items are strings, this works
    result = a + b + c;
    printWithType(result); //123 string

    // - If they were not strings you could do
    result = a.toString() + b.toString() + c.toString();
    printWithType(result); // 123 string

    // Step (2) Convert "123" into 123
    result = parseInt(result,10);
    printWithType(result); // 123 number

    // Step (3) Add 123 + 100 = 223
    result = result + 100;
    printWithType(result); // 223 number

    // Step (4) Convert 223 into "223"
    result = result.toString(); //
    printWithType(result); // 223 string

    // If you concatenate a number with a 
    // blank string, you get a string    
    result = result + "";
    printWithType(result); //223 string
</script>
artlung
+1  A: 

These questions come up all the time due to JavaScript's typing system. People think they are getting a number when they're getting the string of a number.

Here are some things you might see that take advantage of the way JavaScript deals with strings and numbers. Personally, I wish JavaScript had used some symbol other than + for string concatenation.

Step (1) Concatenate "1", "2", "3" into "123"

result = "1" + "2" + "3";

Step (2) Convert "123" into 123

result = +"123";

Step (3) Add 123 + 100 = 223

result = 123 + 100;

Step (4) Convert 223 into "223"

result = "" + 223;

If you know WHY these work, you're less likely to get into trouble with JavaScript expressions.

Nosredna
I actually think this is extremely bad practice because it's fairly opaque as to waht's going on. Knowing that certain operations affect an implicit cast is a trick, and a good one to know, but it's not at all readable. Communication is everything.
annakata
I think it's important to know these things PRECISELY so you know what's going on when you read code. Much JavaScript is terse because it's transmitted as source, and small code can mean server cost reduction.
Nosredna
You should be achieving this through compression tools though, in this form it's optimisation at a very real cost of maintenance.
annakata
Still, knowing these things would help prevent the question in the first place, which was a lack of knowledge on how JavaScript deals with strings and numbers. It's a matter of intellectual curiosity. What happens when I add a number to a string? How does unary plus work? If you don't know the answers, you don't really know JavaScript, and these kind of questions will come up.
Nosredna
A: 

To convert a string to a number, subtract 0. To convert a number to a string, add "" (the empty string).

5 + 1 will give you 6

(5 + "") + 1 will give you "51"

("5" - 0) + 1 will give you 6

Robert L
A: 

parseInt is misfeatured like scanf:

parseInt("12 monkeys", 10) is a number with value '12'
+"12 monkeys"              is a number with value 'NaN'
Number("12 monkeys")       is a number with value 'NaN'

xander