
Cut And
Paste Code For Lesson 2 Semester 2.
Click here to go back to our lessons page.
A Brief example of using the "case" command:
<html>
<body>
<input id="myInput" type="text">
<button onclick="myFunction()">Enter a color</button>
<p id="demo"></p>
<script>
function myFunction() {
var text;
var colors = document.getElementById("myInput").value;
switch(colors) {
case "red":
text = "Red is the color of the sky... On Mars!";
break;
case "blue":
text = "Blue is the color of the sky on Earth.";
break;
case "green":
text = "Green is the color of your nose.";
break;
default:
text = "I have never heard of that color... Remember to use lower case. I'm not too bright.";
}
document.getElementById("demo").innerHTML = text;
}
</script>
</body>
</html>
Review! Using "if" and "else" logic control: