Riddle #3
by js_test
JavaScript
/********************
Riddle #3:
1) Variable b is declared with var.
2) Variable a is not declared with var.
3) 'Call A' now returns 15 (as it should).
4) 'Call B' however, now returns 46 ???
********************/
a = 3;
b = 2;
function line(x) {
a = 5;
var b = 4;
return a*x + b;
}
// Call A:
// correctly returns 15
b = line(a) - b;
alert(b);
// Call B:
// still refuses to behave; returns 46 ???
c = line(a) + b;
alert(c);