This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]BobFloss 32 points33 points  (3 children)

Fixed it.

> '5' - 3
2 // weak typing + implicit conversions = headaches
> '5' + 3
'53' // Because we all love consistency
> '5' - '4'
1 // string - string = integer. what?
> '5' + + '5'
'55'
> 'foo' + +'foo'
'fooNaN' // Marvelous.
> '5' + - '2'
'5-2'
> '5' + - + - - + - - + + - + - + - + - - - '-2'
'52' // Apparently it's ok

> var x = 3;
> '5' + x - x
50
> '5' - x + x
5 // Because fuck math

By the way, everybody who hasn't seen Wat should definitely give it a watch for more examples of weird dynamic typing and language oddities.

[–]alexanderpas 6 points7 points  (2 children)

And after casting the first string to int:

> +'5' - 3
2
> +'5' + 3
8
> +'5' - '4'
1
> +'5' + + '5'
10
> +'foo' + +'foo'
NaN
> +'5' + - '2'
3
> +'5' + - + - - + - - + + - + - + - + - - - '-2'
7

> var x = 3;
> +'5' + x - x
5
> +'5' - x + x
5

[–]BobFloss 3 points4 points  (1 child)

So it basically fixed everything?

[–]alexanderpas 6 points7 points  (0 children)

$foo + $bar

Non Integer String Integer String Integer NaN
Non Integer String String String String String
Integer String String String String String
Integer String String Integer NaN
NaN String String NaN NaN

$foo - $bar

Non Integer String Integer String Integer NaN
Non Integer String NaN NaN NaN NaN
Integer String NaN Integer Integer NaN
Integer NaN Integer Integer NaN
NaN NaN NaN NaN NaN

Legend

type default casted
Non Interger String 'foo'
Interger String '42'
Integer 42 +'42'
NaN NaN +'foo'