Riddle #1
by js_test
JavaScript
/********************
Riddle #1:
1) 'var' is omitted from both variables in the local scope.
2) 'Call A' returns 15 when it should return 17.
3) 'Call B' returns 33 when should return 36.
4) I thought the 2nd call would return 34 (e.g. 5 * 3 + 4 + 15).
********************/
a = 3;
b = 2;
function line(x) {
a = 5;
b = 4;
return a*x + b;
}
// Call A:
// returns 15 when it should return 17
b = line(a) - b;
alert(b);
// Call B:
// returns 33 when should return 36
c = line(a) + b;
alert(c);