Home > database >  How can I run a for loop statement that checks a switch case on every loop?
How can I run a for loop statement that checks a switch case on every loop?

Time:06-03

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>";

}

CodePudding user response:

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();
   }
  }

}

CodePudding user response:

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>";
}
  • Related