Here are the answers:

- a = 0, b=2;

a– == 0 is false, because the “a” is returned before the calculation “–“, as (a–==0) is false, the next part of comparison is cancelled. So b still has value 2, but a becomes 0 - one possible answer:

12345function calculator(t){return t.reduce(function(a,b,i){return eval((i-1)?a+b.join(''):a.join('')+b.join(''));});} - undefined

see var hoisting - true, false, true, true

depending on position of decrement/increment operator, the returned result is different.

ex:

1234var y =5;x = y--; // x ->5, y->4y=5;x = --y; //x ->4, y->4 - function

same as variable hoisting, function declaration is hoisted as well. -
== operator trys to do the type coercion before comparison (regardless of type), but === compare both type and value.

1234567890 == '' //true0 == '0' //truefalse == 'false' //falsefalse == '0' //truefalse == undefined //falsefalse == null //falsenull == undefined //true - ref: Floating Point Basic

Pages: 1 2

For #2, obeying the priority of operators, shouldn’t the value of -5+2*2 be -5+(2*2), i.e. -1?

To make it work properly you need to create the whole equation as a string and eval it all at once, then js will sort out the order of operators itself. So you want something like

var equation=’0′;

for (var index in t) {

equation+=t[index][0]+t[index][1];

}

return eval(equation);

in fact, the caculation should be step by step like a real calculator, the question asks you to do like this:

//step 1

var result = -5+2;

//step 2

result = result * 2;

at first, I didn’t understand the question neither ðŸ™‚