dimanche 24 mai 2020

How do I turn these If else statements into Switch statements

so I have a simple function to see what is checked in the checkboxes, and then spew out the prices. Im trying to make it so i have every possible combination possible as "If" statements so what ever the user chooses, the right price will pop up. I was just wondering How I can turn this code into switch statements to make it for efficient. and if theres anything else thats inefficient I should fix, I would love feedback, this is my first week using JS for a school project.

    <!DOCTYPE html>
<html>

<head>
<script>
function testFunc() {
  cost = 0;
  var isOne = document.getElementById('Maui').checked;
  var isTwo = document.getElementById('Oahu').checked;
  var isThree = document.getElementById('Hawaii').checked;


  if(isOne) {
    cost += 1350
  }
  if(isOne && isTwo) {
    cost = 1850
  }
  if(isOne && isTwo && IsThree) {
    cost = 2100
  }
  else{
    cost = 1350
}
  if(isTwo){
    cost += 0
  }

  document.getElementById('cost').innerHTML = cost;
}

</script>
</head>

<body>

<h2>Chose Islands For Package/h2>

<form>
  <input type="checkbox" id="Maui">

  <label>Maui</label><br>

  <input type="checkbox" id="Oahu">

  <label>Oahu</label><br>

  <input type="checkbox" id="Hawaii">

  <label>Hawaii</label><br>


</form>

 <button onclick="testFunc()">Price </button>

 <p id="cost"> </p>

</body>
</html>

Aucun commentaire:

Enregistrer un commentaire