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

207
Views
¿Cómo verificar si indexOf es par o impar?

Quiero agregar un estilo a los elementos pares e impares de esta matriz.

 let myArr = ["a", "b", "c", "d", "e", "f"]; const btn = document.querySelector("#btn"); const body1 = document.body; let i = 0; btn.addEventListener("click", function () { i++; let newDiv = document.createElement("div"); newDiv.textContent = myArr[i]; body1.appendChild(newDiv); });
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

La forma más simple y eficiente de determinar si un número es par o impar:

 number & 1
about 4 years ago · Juan Pablo Isaza Report

0

Deberá recorrer la matriz para obtener el índice. Usando eso, puede hacer la verificación del módulo para ver si es par o impar y hacer algo con el div antes de empujarlo hacia el cuerpo.

 document.addEventListener('DOMContentLoaded', () => { let myArr = ["a", "b", "c", "d", "e", "f"]; const btn = document.querySelector("#btn"); const body1 = document.body; // Start at 1 to avoid any + 1 in the modulus check for(let i = 1; i <= myArr.length; i++) { const newDiv = document.createElement("div"); newDiv.style.width = '100px' newDiv.style.height = '100px' newDiv.style.margin = '5px' newDiv.style.color = 'white' // - 1 because we start the loop at 1 and indexes start at 0 in JS newDiv.textContent = myArr[i - 1]; if (i % 2 === 0) { newDiv.style.backgroundColor = 'red' newDiv.style.float = 'right' } else { newDiv.style.backgroundColor = 'blue' newDiv.style.float = 'left' } body1.appendChild(newDiv); } })

about 4 years ago · Juan Pablo Isaza Report

0

Supongo que los elementos de la matriz son elementos DOM. Así que tal vez quieras hacer algo como esto:

 arr.forEach((item, index) => { if (index % 2 == 0) { // Even item.style = ...; // or maybe item.className = ...; } else { // Same here, apply whatever style/class you want to use for the odd elements. } });
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!