- 2

Good morning sir. I have error line 13. Thanks you sir.

I have error line 13 https://code.sololearn.com/Wz2zd7JqqMn8/?ref=app

29th Jan 2022, 10:55 AM
Malick Diagne
Malick Diagne - avatar
3 Respuestas
0
Variables must be decleared before using them, in line 13 you used i but it wasn't decleared, why to use a for loop in line 15 ? This for loop is usless, since it will assign the last color which is yellow to the paragraph style, further more every time you click, if i is decleared as following, var i=0; it will assign color red to the paragraph which makes the for loop completely waste of time.
29th Jan 2022, 11:28 AM
Mohammed
+ 2
// This can be done as follows: var i = 0; function addColor (){ var el = document.querySelector (".yellow"); let colors = ["red","blue","yellow"]; if (i < colors.length ) el.style.color = colors [i]; else i = -1; i++; }
29th Jan 2022, 1:03 PM
JaScript
JaScript - avatar
0
Declearing variables outside the function is better, because every time click button works it will assign the same variables, colors array and el, therefore the function will take up lesser time to excute. Declare i=0 outside the function. Inside the function apply the following: Every time you click, i should be increased by 1, every time i==colors.length the i should be 0. el.style.color= colors[i];
29th Jan 2022, 11:50 AM
Mohammed