0
How do I make the color go back to red and continue in a loop.
8 Antworten
+ 3
The easiest way is
Instead
x—;
you write:
x = 0;
+ 3
Abdul-Quadri if you want to cycle over the array, you could simply do:
x = ++x%arr.length;
div.style.background = arr[x];
and avoid if..else ;)
+ 2
check this:
https://code.sololearn.com/WNUzgRr2Bth2/?ref=app
+ 2
Welcome.
+ 1
JaScript Thanks
+ 1
The code itself is correct, you do not need any further manipulations.
You just need to reset the x variable in the else statement.
if(x < arr.length){
x++;
div.style.background = arr[x];
} else if(x >= arr.length){
x--; // => better change this to 0, zeroing or reset x value.
div.style.background = arr[x];
}
Just like that. X should be 0.
0
visph Thanks for that
0
Hamed Ganji Thanks