1
Consider the Javascript code below.
var r = [2, 5, 6, 18, 20, 10, 23, 12, 19, 10];
var s = [1, 5, 7, 13, 18, 21, 10, 25, 32, 17, 3];
var x = [0];
var i;
for (i = 0; i <= 9; i++) {
x[i] = r[i];
}
for (i = 0; i <= 10; i++) {
x[i + 10] = s[i];
}
document.write(x[13] + "<br>");
document.write(i);
Values will be displayed on the screen
- (A) 13 and 10.
- (B) 7 and 11.
- (C) 18 and 10.
- (D) 13 and 11.
- (E) 7 and 10.
Answer:
Answer: Letter D
I ran the code and everything OK, the answer is correct, but I was in doubt in the letter To and curled up in the for (i = 0; i <= 10; i++)
. Why are you incrementing once more and leaving i=11
instead of i=10
? I ran the same function in java and if I do
for(int i = 0 ; i <= 10 ; contador = ++i) contador=11
for(int i = 0 ; i <= 10 ; contador = i++) contador=10
This last example does not equal the example of the question?
The variable
i
is incremented after executing the blockfor
, then, after the last valid execution for the blockfor
, will still run the last increment. However, it will not fall in the clausei <= 10
. Soon you’ll break the loop offor
.– LipESprY
The result is 11 for the
for
will add up the valuei+1
(ini++
) and only then will you check the conditioni <= 10
.– Valdeir Psr
I believe that is answered here, in particular the item
m)
of the response containing the step by step I can’t learn the syntax offor
– Bacco