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

174
Views
Necesita repetir la iteración de la matriz para hacer el texto del arco iris

Este código que escribí diseña los caracteres de la entrada del usuario en un arcoíris.

 function button(){ var name = window.prompt("Insert your name"); var namearray = []; namearray = Array.from(name); let colors = ["red","orange","yellow","green","blue","purple"]; for(i=0; i < namearray.length; i++){ element = document.createElement("p"); elementtext = namearray[i]; element.innerHTML = elementtext; document.getElementById("namediv").appendChild(element); element.style.color = colors[i]; } }

El problema es que solo colorea palabras de hasta 6 caracteres. Lo que pensé fue repetir la iteración de la matriz de colores para colorear el resto de la entrada, pero no sé cómo aplicarlo.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Utilice el operador % :

 element.style.color = colors[i % colors.length];
about 4 years ago · Juan Pablo Isaza Report

0

cuando ingresa una cadena más grande que la matriz de colores, accederá a un índice que no existe. necesitas tomar el primer color y moverlo hasta el final:

 var name = window.prompt("Insert your name"); var namearray = []; namearray = Array.from(name); let colors = ["red","orange","yellow","green","blue","purple"]; for(i=0; i < namearray.length; i++){ element = document.createElement("p"); elementtext = namearray[i]; element.innerHTML = elementtext; document.getElementById("namediv").appendChild(element); const currentColor = colors.shift(); // take the first color colors.push(currentColor); // push it to the end of the array element.style.color = currentColor; }

aquí hay un ejemplo de trabajo: https://jsfiddle.net/oznw9qeh/

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!