Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

194
Vistas
How can I run a for loop statement that checks a switch case on every loop?

I am still learning JS and I'm not that far into it. I need help connecting my loop statement to check a switch case on every loop and display it every time it loops. I need the loop to check for the month number and then display the case for that number, but it needs to display every month.

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 Respuestas
Responde la pregunta

0

Inside your loop you just changing monthChecker variable, but not calling switch statement.
So monthChecker will be changed 3 times in loop and after that will called switch only with last month

Call your switch and displaying code inside loop like this:

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 Denunciar

0

You can simply return an object from the switch case.

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda