Home > Blockchain >  One if statement doesn't work and the rest do (Javascript)
One if statement doesn't work and the rest do (Javascript)

Time:10-02

So, I'm working on a game and the way to level up is through the money variable. I decided to use an interval and run the function upc every 0.099 seconds and when I run the code, you can get up to about at the point where it says if(money > 1000005 && Cookies.get*("modal2alrshown"). The Cookies.get is just a JS library.

function upc(){
  if(money > 1000){
    irp.style.display = "inline";
    irptxt.style.display = "inline";
    irpbtn.style.display="inline";
   } else {
    return false
  }
  if(money > 1005 && Cookies.get("modal1alrshown") != "yep"){
    npamodal("./assets/iron_pickaxe.png","Iron Pickaxe")
    Cookies.set("modal1alrshown","yep");
  }
  if(money > 1000000){
    gp.style.display = "inline";
    gptxt.style.display = "inline";
    gpbtn.style.display="inline"
  } else {
    return false;
  }
  if(money > 1000005 && Cookies.get("modal2alrshown") != "yep"){
    npamodal("./assets/gold_pickaxe.png","Gold Pickaxe")
    Cookies.set("modal2alrshown","yep");
  } else {
    return false;
  }
  if(money > 5000000){
    bgep.style.display = "inline";
    bgptxt.style.display = "inline";
    bgpbtn.style.display="inline";
  } else {
    return false;
  }
  if(money > 5000005 ){
    upcontent.innerHTML = "<div id='up1-close'>To exit, click on sky</div><h1 style='text-align:center;'>New Pickaxe Availible!</h1><img src='./assets/baguettepick.png' height='120px'>Baguette Pickaxe<br>And you got a <br><h3>Travel Ticket<img src='./assets/travelticket.png' height='20px'></h3>"
    $("#up1-content, #up1-background").toggleClass("active");
    Cookies.set("modal3alrshown","yep");
    Cookies.set("travelTicket","1");
  } else {
    return false;
  }
} 

CodePudding user response:

It could be that one of your if statements that return from the function have a false expression

// Potentially this is the faulty expression
if(money > 1000005 && Cookies.get("modal2alrshown") != "yep"){
    ....
} else { 
    // Causing this return to fire, exiting the function
    return false;
}

CodePudding user response:

If statement will always work. You need to debug that either money > 1000005 to be true or Cookies.get("modal2alrshown") != "yep" should be true.

  • Related