+ 7
Why is it that some loops are just giving single answer?
I know I loop repeats something a number of time, where do we have one answer for some loops? Am referring to JavaScript To those of you who want an example, I came across this example today: Var x=1; for(;x<6; x+=2){ x=x*x; } document. write(x); //Ans is 11,why one value?
9 Antworten
+ 7
No idea without seeing your code.
+ 7
I think you talk about the output of a code, you need to enter the value at the exit of the loop. You just need to identify the syntax and to be used with. 😌 I wish I helped you. 🙋♂️
+ 6
if you console log loop accumulator value out of the loop thats also reason for it show your codes it will make it easy to explain.
+ 3
can you show us an example of that loop?
+ 3
A loop can return a list or array (only one) 🤔
+ 3
In your code , it is a for loop with condition x<6 and next is increment x value by 2.
var x=1
for (; x<6; x+=2) {
x=x*x
}
document.write(x)
Here first iteration it is checking condition that is x less than 6 , then incrementing its value by 2 , that is 1+2 is 3 next x=3*3
then x=9, in next iteration it skipping condition as x is greater than 6 and simply updating it by 2 that is 11.
If you try by increasing value of x by 3 it will give output 19,
like for(; x<6; x+=3)
+ 1
Yes that's it Francis Woli
0
If I understand you well, your are worried as to why your code is not displaying all your outputs.
That is because the document.write(x) is outside the loop and so only the final value of the loop would be assigned to x after the loop.
// Try this
Var x=1;
for(;x<6; x+=2){
x=x*x;
document. write(x);
}
0
Put write statement inside loop