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

138
Views
quiero que la función de for loop se realice una por una cuando se presiona una tecla o el mouse
<div id="text">Nature in the broadest sense is the physical world or universe</div> let el = document.getElementById(text); let eltext = el.innerText; let final = eltext.split(" "); let i; for (i = 0; i < final.length; i++) { if (final[i] === "the") { final[i] = `<b>${final[i]}</b>`; } let result = (final + " ").replace(/,/g, " "); el.innerHTML = result;

el resultado será: La naturaleza en el sentido más amplio es el mundo físico o universo

lo que quiero: cuando cualquier tecla se activa por primera vez: la naturaleza en el sentido más amplio es el mundo físico o el universo

cuando cualquier tecla se activa por segunda vez: la naturaleza en el sentido más amplio es el mundo físico o universo

cuando cualquier tecla se activa por tercera vez: la naturaleza en el sentido más amplio es el mundo físico o el universo

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

0

En lugar de for loop, le recomiendo que use métodos de Array integrados para una mejor legibilidad. Aquí he extendido su oración de muestra para mostrar cómo puede adaptarse a oraciones más largas con más palabras objetivo.

 const text = document.querySelector('#text').innerText.split(' '); const indices = text.map((word, index) => word === 'the' ? index : 0) .filter(i => i !== 0); let highlightIndex = 0; function toggle() { const arr = [...text]; arr[indices[highlightIndex]] = `<strong style="color: red;">${arr[indices[highlightIndex]]}</strong>`; document.querySelector('#text').innerHTML = arr.join(' '); highlightIndex += 1; if (highlightIndex > indices.length -1 ) highlightIndex = 0; } window.addEventListener('keydown', toggle); window.addEventListener('click', toggle);
 <div id="text">Nature in the broadest sense is the physical world or universe. Introduce the article here for testing the code.</div>

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!