External Exam Download Resources Web Applications Games Recycle Bin

Cases

switch.js

floor = 2;

switch(floor){
        
    case 1:
        alert("Bottom floor");
        break;

    case 2:
        alert("Middle floor");
        break;
        
    case 3:
        alert("Top floor");
        break;
        
}

case.js

//I'm not sure how much my piece is worth yet,
//so I'll start it off at negative one:
piece_value = -1;
    
piece = "Knight";

switch(piece.toUpperCase()){
        
    case "KNIGHT": case "BISHOP":
        piece_value = 3;
        break;

    case "ROOK":
        piece_value = 5;
        break;
        
    case "QUEEN":
        piece_value = 9;
        break;
        
    default:
        //all other PAWNS are worth 1:
        piece_value = 1;
        break;
}
    
alert(piece + " value: " + piece_value);

trick.js

dealer = 18;
    
switch (true) {
    case (dealer <= 16):
        alert("Hit card!");
        break;
    case (dealer <= 21):
        alert("Stay / sit.");
        break;
    default:
        alert("Bust :-(");
        break;
}
  1. Create a variable called lightColour, and set its value to "Red", "Green" or "Yellow". Use a switch case to alert what action I should take. If the light is "Red", your program should alert "Stop". If the light is "Yellow", your program should alert "Wait". If the light is "Green", your program should alert "Go".

  2. Use a switch case to give a complement on a grade. So if I scored an "A", output "Aced it!". If I scored a "B", output "Good result". If I scored a "C", output "You passed!". For a "D", output "Good try", for an E, output "Never mind".

  3. Create a program that inputs a year level (as an integer), then tells me where that year level is meant to go, based on this announcement in the morning notices: "All year 8 and 11 students go the hall. All year 12 students go to the library. Everyone else goes to their PC rooms." Use switch / case / default in your answer.