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

203
Views
¿Cómo puedo ejecutar una declaración de bucle for que verifique un caso de cambio en cada bucle?

Todavía estoy aprendiendo JS y no estoy tan metido en eso. Necesito ayuda para conectar mi declaración de bucle para verificar un caso de cambio en cada bucle y mostrarlo cada vez que se repite. Necesito el ciclo para verificar el número del mes y luego mostrar el caso de ese número, pero debe mostrarse todos los meses.

 function display(){ const months = [1, 2, 3]; let monthChecker = ""; for (let i = 0; i < months.length; i++) { monthChecker = months[i]; } switch (monthChecker) { case 1: monthName = "January"; holiday1 = "New Years" holiday2 = "MLK Day" break; case 2: monthName = "February"; holiday1 = "Valentines" holiday2 = "President's Day" break; case 3: monthName = "March"; holiday1 = "Ash Wednesday" holiday2 = "St. Patrick's Day" break; default: monthName = "Not a valid month number"; holiday1 = ""; holiday2 = ""; } document.getElementById("monthName").innerHTML = monthName + " holidays include: " + holiday1 + " and " + holiday2 + "<br>";

}

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

0

Dentro de su ciclo, solo cambia la variable monthChecker , pero no llama a la declaración de cambio.
Por lo tanto, monthChecker se cambiará 3 veces en bucle y luego se llamará al cambio solo con el mes pasado

Llame a su interruptor y muestre el código dentro del bucle como este:

 let monthName; let holiday1; let holiday2; function getMonthData(monthChecker) { switch (monthChecker) { case 1: monthName = "January"; holiday1 = "New Years" holiday2 = "MLK Day" break; case 2: monthName = "February"; holiday1 = "Valentines" holiday2 = "President's Day" break; case 3: monthName = "March"; holiday1 = "Ash Wednesday" holiday2 = "St. Patrick's Day" break; default: monthName = "Not a valid month number"; holiday1 = ""; holiday2 = ""; } } function renderMonthData() { document.getElementById("monthName").innerHTML = monthName + " holidays include: " + holiday1 + " and " + holiday2 + "<br>"; } function display() { const months = [1, 2, 3]; for (let i = 0; i < months.length; i++) { getMonthData(months[i]); if (monthName !== "Not a valid month number") { renderMonthData(); } } }
about 4 years ago · Juan Pablo Isaza Report

0

Simplemente puede devolver un objeto de la caja del interruptor.

 const months = [1, 2, 3]; function checkMonth(x) { switch (x) { case 1: return { monthName: "January", holiday1: "New Years", holiday2: "MLK Day", }; case 2: return { monthName: "February", holiday1: "Valentines", holiday2: "President's Day", }; case 3: return { monthName: "March", holiday1: "Ash Wednesday", holiday2: "St. Patrick's Day", }; default: return { monthName: "Not a valid month number", holiday1: "", holiday2: "", }; } } for (let i = 1; i <= months.length; i++) { document.getElementById(checkMonth(i).monthName).innerHTML = checkMonth(i).monthName + " holidays include: " + checkMonth(i).holiday1 + " and " + checkMonth(i).holiday2 + "<br>"; }
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!