recently started re-doing an online course WDB2.0 on Udemy.
The objective was to make each letter from array of letters RAINBOW coloured according to rainbow colors using javascript selector.
const colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']; html code : https://pastebin.com/uDAA1UaP
I have managed to complete the exercise by writting the following code:
const rainbow= document.querySelectorAll("span");
for (let letter of rainbow){
letter.style.color=colors[0];
colors.shift()
}
My question is why did none of the following codes worked? Both of them have colored the whole text in violet, rather than each letter according to the index of the colors array: 1)
for (let i = 0; i<colors.length; i++){
for(let letter of rainbow){
letter.style.color=colors[i]
}
}
for (let letter of rainbow){
for(let i = 0; i<colors.length; i++){
letter.style.color=colors[i]
}
}
I have always struggled with double loops and would really appreciate if you could explain what am I doing incorrectly.
Many thanks!