+ 1

What is going on with alert("1" - - "1")

I just had this in a challenge and I don't know this. How is this called, when to use this. I am not referring to the alert, just the "1"- -"1" the solution seems to be 2

29th May 2017, 5:57 PM
DeKar
DeKar - avatar
7 Antworten
+ 4
The answer is error because you cannot stack JavaScript's operators with spaces in between , The code should be put this way if you want output to be 2. alert ("1" - "-1"); This gives 2 on codes playground
29th May 2017, 6:48 PM
Nomeh Uchenna Gabriel
Nomeh Uchenna Gabriel - avatar
+ 4
@ Maart Try this one to fool the console. var a = 1; var b = 2; console.log ( a-=-b); this will output 3. and it also means (a = a -- b) in real world maths you can also stack everything together for a clean output; console.log (1--2); because console.log (1 - - 2); outputs an error because of those spaces in between the characters
29th May 2017, 7:02 PM
Nomeh Uchenna Gabriel
Nomeh Uchenna Gabriel - avatar
+ 3
@Maart Ok edited
29th May 2017, 7:27 PM
Nomeh Uchenna Gabriel
Nomeh Uchenna Gabriel - avatar
+ 2
In addition to @Nomeh a double negative means positive, so 1 - - 2 is the same as 1 + 2
29th May 2017, 6:52 PM
Maart
Maart - avatar
+ 2
https://code.sololearn.com/W0L56Cyw1zU9/?ref=app it works on playground, and was like this on a js challenge, that's why I am confused. Oh and also, it is OK to math string numbers without parsing them first?
29th May 2017, 8:40 PM
DeKar
DeKar - avatar
+ 1
Fair. Was explaining the basic maths behind it though. Wasn't sure if he knew about that.
29th May 2017, 7:20 PM
Maart
Maart - avatar
+ 1
I would say no, but it does seem to work here...
29th May 2017, 8:55 PM
Maart
Maart - avatar