+ 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
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
+ 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
+ 3
@Maart Ok edited
+ 2
In addition to @Nomeh a double negative means positive, so 1 - - 2 is the same as 1 + 2
+ 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?
+ 1
Fair. Was explaining the basic maths behind it though. Wasn't sure if he knew about that.
+ 1
I would say no, but it does seem to work here...