Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

195
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda