How do I make the color go back to red and continue in a loop. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do I make the color go back to red and continue in a loop.

https://code.sololearn.com/WG2wlOzJa7GL/?ref=app

6th Jun 2021, 5:23 AM
Abdul-Quadri
8 Answers
+ 3
The easiest way is Instead x—; you write: x = 0;
6th Jun 2021, 6:03 AM
JaScript
JaScript - avatar
+ 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 ;)
6th Jun 2021, 6:06 AM
visph
visph - avatar
6th Jun 2021, 5:34 AM
visph
visph - avatar
+ 2
Welcome.
6th Jun 2021, 9:15 PM
JaScript
JaScript - avatar
+ 1
JaScript Thanks
6th Jun 2021, 7:21 PM
Abdul-Quadri
+ 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.
7th Jun 2021, 7:30 AM
Hamed Ganji
Hamed Ganji - avatar
0
visph Thanks for that
6th Jun 2021, 7:21 PM
Abdul-Quadri
0
9th Jun 2021, 4:51 PM
Abdul-Quadri