Javascript switch statement Question 1

Introduction

We would like to convert number to month name using switch statement.

For example, convert 4 to April.

let monthNum = 4; 
let monthName; 
//your code here

console.log(monthName);


let monthNum = 4; 
let monthName; 
switch (monthNum) { 
   case "1": 
      monthName = "January"; 
      break; 
   case "2": 
      monthName = "February"; 
      break; 
   case "3": 
      monthName = "March"; 
      break; 
   case "4": 
      monthName = "April"; 
      break; 
   case "5": 
      monthName = "May"; 
      break; 
   case "6": 
      monthName = "June"; 
      break; 
   case "7": 
      monthName = "July"; 
      break; 
   case "8": 
      monthName = "August"; 
      break; 
   case "9": 
      monthName = "September"; 
      break; 
   case "10": 
      monthName = "October"; 
      break; 
   case "11": 
      monthName = "November"; 
      break; 
   case "12": 
      monthName = "December"; 
      break; 
   default: 
      console.log("Bad input"); 
} 



PreviousNext

Related