+ 3
I really need a answer and a practice link to fully understand loop like this in JavaScript
var x =[12345] for(var i=0; i<4;i++){ x[i]=x[i+1] if(i==x.length-2){ x[i]=x[0] } } alert(x)
13 Respuestas
+ 7
George S Mulbah right
I forgot JS cra... logic.
XD
+ 5
George S Mulbah I would guess
I don't know JavaScript.
+ 4
George S Mulbah
if i is equal to x's length minus 2
+ 3
LONGTIE will the array length be reduce by 2
+ 3
thanks mahn but this course is way more complicated because eg ("5"-1)=4 but ("5"+1)=51 you see why we can not assume any thing in JavaScript accept we know it
+ 2
It is code for rotate array to the left and finally after if() it adding first element to the end of array.
Try this correct version with output for each step of changing array:
var x =[1,2,3,4,5];
var x0 = x[0];
document.write(x +"<br>for");
for(var i=0; i<4; i++){
x[i] = x[i+1];
if(i==x.length-2){
document.write("<br>"+ x +"<br>if");
x[i+1]=x0;
}
document.write("<br>"+ x);
}
alert(x);
/* output:
1,2,3,4,5
for
2,2,3,4,5
2,3,3,4,5
2,3,4,4,5
2,3,4,5,5
if
2,3,4,5,1
*/
+ 1
Choubada ,Sir Can you elaborate more on your answer please, I am still not understanding
+ 1
the variable named i, in our loop will be our array indexer.
The begin of our loop:
i = 0
so x[i] equals to 1.
etc etc..
And, for each number in the array, we add one to it.
So, after the loop, x is equal to [2,3,4,5,6]
+ 1
thank a lot ,so we are incrementing each number in the array by 1 is that right ,upon the execution of our loop
+ 1
but what is the meaning of
if(i==x.length-2)
0
X has one only index, you should use , between yout numbers.
Your loop:
So, for each element of array x, assign its value to the next element of the array.
Pretty simple :)
0
zemiptark could you
0
zemiah could you elaborate more on the for loop and if statement