vendredi 30 avril 2021

Js how to write multiple ifs in a efficient way?

I am still a beginner to JS and I am kinda struggling on how to write multiple ifs in a correct way. For example I wrote something like this:

function calculatespot() {
  //Spot 1 to 2 transfer bandage
  if (spot1Selected == 1 && spot2Selected == 1) {
    if (spot2Free == 1) {
      localStorage.setItem('spot1Free', 1)
      localStorage.setItem('spot2Free', 0)
      localStorage.setItem('spot1Selected', 0)
      localStorage.setItem('spot2Selected', 0)
      document.getElementById('block1').style.backgroundColor = "#9eafa6"
      document.getElementById('block2').style.backgroundColor = "#9eafa6"
      if (user_item1 == "Bandage") {
        localStorage.setItem("slot1Type", "")
        localStorage.setItem("slot2Type", "Bandage")
        document.getElementById('inventoryactionbtn').style.visibility = "Hidden"
        document.getElementById('item1').src = "/static/images/transparant.png"
        document.getElementById('item2').src = "/static/images/bandage.png"
        localStorage.setItem('slot1Type', "")
        localStorage.setItem('slot2Type', "Bandage")
      }
    }
  }

This is not a very good way, but I still need all those points to match before executing the code. How could I write something like this in a better and more efficient way without having to nest all those ifs?

Aucun commentaire:

Enregistrer un commentaire