+ 5
In first program res is assigned 30 and then 0 and second is alerted.
Second program sums the digits of 1234. And outputs 10 as result.
+ 9
in the second program
-1 + 1 = 0
+ 8
var res = inc (29):
Assign 30 to res. But it's not alerted now
(a != !true) is a true condition and "if" block is executed. So res = inc (-1) assigns 0 to res. Finally res with value of 0 is alerted.
In second code we parse digits of 1234 as follow:
a%10 outputs the least significant digit of number: 1234%10 = 4. Then we add 4 to sum.
Now we must take next digit so we reduce 1234 to 123 as follow:
First we subtract the least significant digit:
1234-1234%10 = 1230
And then divide the result by 10:
1230/10= 123
As you see one digit has been reduced.
This process continues until all digits add to sum and "a" will be zero and while loop terminates.
+ 7
You right.
In the first program res = inc(29) will 30. But if block is executed and res = inc(-1) will 0.
And finally alert(res). 0
+ 6
Mofey how you think what is output for this code.
var a = 5;
a = 1;
Alert (a);
?
+ 6
I hope you understand. đđđ
+ 3
inc (-1) calls inc function with "-1" as argument.
inc increments the argument by one and returns it. So (-1)+1=0.
This value overwrites previous value of res (30). So when wecall alert, 0 is shown.