Q:

how to do a switch function javascript

let color = "black";

switch(color){
    case "red":
        console.log("color is red");
        break;
    case "white":
        console.log("color is white");
        break;
    case "black":
        console.log("color is black");
        break;
    default:
        console.log("unknow color");
}
2
function caseInSwitch(val) {
  var answer = "";
  // Only change code below this line
switch(val) {
  case 1:
    answer = "alpha";
    console.log(answer);
    break;
  case 2:
    answer = "beta";
    break;
  case 3:
    answer = "gamma";
    break;
  case 4:
    answer = "delta";
    break;
}


  // Only change code above this line
  return answer;
}

caseInSwitch(1);
5
switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}
44

New to Communities?

Join the community