Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

394
Views
Looping through arrays. Rainbow Text Exercise

Need help. I attached the image of the question, This is the image of the code:

// This is the given code:

const colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'];
// I written these:

const spans = document.querySelector('h1 span');

for(let rainbow of spans){
    spans.style.color = " ";
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You should use document.querySelectorAll('h1 span') to get all span item.

Then you can use Array.forEach to get character and index. Then you map the index to the colors array.

// This is the given code:
const colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'];

// I written these:
const spans = document.querySelectorAll('h1 span');
spans.forEach((item, index) => {
  item.style.color = colors[index];
})
<h1>
  <span>R</span>
  <span>A</span>
  <span>I</span>
  <span>N</span>
  <span>B</span>
  <span>O</span>
  <span>W</span>
</h1>

about 4 years ago · Juan Pablo Isaza Report

0

const colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']; 
//PLEASE DON'T CHANGE THIS LINE!

//YOU CODE GOES HERE:

const letters = document.querySelectorAll('span')enter code here

let count = 0

for (let letter of letters){
    letter.style.color = colors[count]
    count += 1
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!