That’s a code fragment task – you should enter “var” (as many as want) in it in order to get 17 in the first, and 21 in the second alert. I thing that I have met this before, but still was not able to solve the issue.
a = 3;
b = 2;
function line(x) {
a = 5;
b = 4;
return a*x + b
}
//b should be 17
b = line( a ) - b;
alert( b );
//c should be 21
c = line ( a ) + b;
alert(c);
If you put “var” in the function in front of b, it will alert “17”. The next alert gives us 46 because of the new value of b, return by the function.
function line(x) {
a = 5;
var b = 4;
return a*x + b
}
That’s the source of the task:
Using exactly what’s given, in exactly the way it’s given is impossible.
What I mean by that is if the call:
is dependent upon the value of b which is the assignment at:
Then it’s 100% impossible to either have made
aa significantly-small number, or madeba significantly-large negative number to make the math work.Therefore it’s my belief that they’re intended to be two separate checks.
Best-case scenario, if we’re trying to have
b=17included:That’s the smallest you’re going to get, assuming you run the two back-to-back.
Even if you did it that way, you wouldn’t get
b = line(a) - b = 17on the first run…If it was written:
Then you could run both in succession and get the expected result.
Or you can run:
and get 17.
Then you can run:
(ie: the exact same setup with a different instigator, and without the saved
bvalue from the return of the previous call), and get the desired result.But it’s not possible to run both of them one after the other, and expect to have them both work, without doing anything to the code but add a
varor four.