Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
JavaScript - Switch Break Statements
#1
The HTML part between the <body></body> tags looks like this:

Code:
What's your favorite fruit?
<br>
<input type="text" id="fruit"></input>
<br>
<input type="button" id="submit" onclick="submit()" value="submit"></button>
<br>
<p id="output"></p>

The JavaScript part looks like this:

Code:
function submit()
{
  fruitType = document.getElementById("fruit").value;
 
  switch(fruitType)
    {
      case "Oranges":
        document.getElementById("output").innerHTML = "Oranges are $0.59 a pound.";
        break;
      case "Apples":
        document.getElementById("output").innerHTML = "Apples are $0.32 a pound.";
        break;
      case "Bananas":
        document.getElementById("output").innerHTML = "Bananas are $0.48 a pound.";
        break;
      case "Cherries":
        document.getElementById("output").innerHTML = "Cherries are $3.00 a pound.";
        break;
      case "Mangoes":
        document.getElementById("output").innerHTML = "Mangoes are $0.56 a pound.";
        break;
      case "Papayas":
        document.getElementById("output").innerHTML = "Papayas are $2.79 a pound.";
        break;
      default:
        document.getElementById("output").innerHTML = `Sorry, we ran out of ${fruitType}. <br> Would you like to get anything else?`;
    }
}
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Messages In This Thread
JavaScript - Switch Break Statements - by Qomplainerz - 04-04-2023, 07:47 AM

Forum Jump:


Users browsing this thread: 3 Guest(s)