mardi 19 octobre 2021

My if statement isn't showing/hiding the element and I don't know why

I have an if statement at the end of my js and whenever I get past the last question onto the results screen, the element for L is the only one that shows and W never shows even if total is = to 4. This is coded on applab, so not sure if that changes anything. I don't know exactly what I'm doing wrong since I'm a newbie but any help would be greatly appreciated.

//Welcome screen
onEvent("Welcome", "click", function() {
  setScreen("Q1");
  console.log("Welcome was clicked");
});
var total = 0;
//Question one
onEvent("q1a1", "click", function() {
  setScreen("Q2");
  console.log("Question was answered correctly!");
  total++;
});
onEvent("q1a2", "click", function() {
  setScreen("Q2");
  //on answers where the response is wrong, nothing will be added to total
});
onEvent("q1a3", "click", function() {
  setScreen("Q2");
});
onEvent("q1a4", "click", function() {
  setScreen("Q2");
});

//Question two
onEvent("q2a1", "click", function() {
  setScreen("Q3");
});
onEvent("q2a2", "click", function() {
  setScreen("Q3");
  total++;
});
onEvent("q2a3", "click", function() {
  setScreen("Q3");
});
onEvent("q2a4", "click", function() {
  setScreen("Q3");
});

//Question three
onEvent("q3a1", "click", function() {
  setScreen("Q4");
});
onEvent("q3a2", "click", function() {
  setScreen("Q4");
});
onEvent("q3a3", "click", function() {
  setScreen("Q4");
});
onEvent("q3a4", "click", function() {
  setScreen("Q4");
  total++;
});

//Question four
onEvent("q4a1", "click", function() {
  setScreen("Q5");
  total++;
});
onEvent("q4a2", "click", function() {
  setScreen("Q5");
});
onEvent("q4a3", "click", function() {
  setScreen("Q5");
});
onEvent("q4a4", "click", function() {
  setScreen("Q5");
});

// Question five
onEvent("q5a1", "click", function() {
  setScreen("Q6");
});
onEvent("q5a2", "click", function() {
  setScreen("Q6");
  total++;
});
onEvent("q5a3", "click", function() {
  setScreen("Q6");
});
onEvent("q5a4", "click", function() {
  setScreen("Q6");
});

//Question six
onEvent("q6a1", "click", function() {
  setScreen("Results");
});
onEvent("q6a2", "click", function() {
  setScreen("Results");
  total++;
});
onEvent("q6a3", "click", function() {
  setScreen("Results");
});
onEvent("q6a4", "click", function() {
  setScreen("Results");
});

//Results into finale  
if (total == 0) {
  showElement("L");
  hideElement("W");
} else if (total == 1) {
  showElement("L");
  hideElement("W");
} else if (total == 2) {
  showElement("L");
  hideElement("W");
} else if (total == 3) {
  showElement("L");
  hideElement("W");
} else if (total == 4) {
  showElement("W");
  hideElement("L");
} else if (total == 5) {
  showElement("W");
  hideElement("L");
} else {
  showElement("W");
  hideElement("L");
}

Aucun commentaire:

Enregistrer un commentaire