Riddle #4
by js_test
JavaScript
/********************
Riddle #4:
1) 'var' is omitted from both variables in the local scope.
2) an alert() is added after the function as a milestone.
3) alert() returned 19 as expected.
4) 'Call B' returns 33 as it did for Riddle #1
5) 'Call A' however, returns 25 ???
6) I figured 'Call A' would return 15 as it did for my 1st riddle but for some reason, my alert() call affected 'Call A'
********************/
a = 3;
b = 2;
function line(x) {
a = 5;
b = 4;
return a*x + b;
}
// returns 19 as expected
alert(line(a));
// Call A:
// unexpectedly returns 25 ???
b = line(a) - b;
alert(b);
// Call B:
// returns 33, as it did in Riddle #1
c = line(a) + b;
alert(c);