简体   繁体   中英

How do I create an if statement inside another if statement with javascript?

I am making a poker game with HTML CSS and Javascript. Right now, I am making the part of the program that actually deals the cards. I need to have an if statement inside another if statement. When it runs through, it generates a random number, then I have a list of if statements that see first if the card has been chosen, then I have another if statement inside of the first if statement to see if the card has been dealt. I don't know why it won't work. Can anyone help? Here is the code:

var yourchipsvar = 500;
var alchipsvar = 500;
var potvar = 0;
var whotodealto = 0;

yourchipsvar = Number(yourchipsvar);
alchips = Number(alchipsvar);
potvar = Number(potvar);

document.getElementById("yourchips").innerHTML = Number(yourchipsvar);
document.getElementById("alchips").innerHTML = Number(alchipsvar);
document.getElementById("pot").innerHTML = Number(potvar);


function bet() {

  yourchipsvar = yourchipsvar - 5;
  potvar = potvar + 5;
  yourchipsvar = Number(yourchipsvar);
  potvar = Number(potvar);
  document.getElementById("pot").innerHTML = potvar;
  document.getElementById("yourchips").innerHTML = yourchipsvar;
}

function bet15() {

  yourchipsvar = yourchipsvar - 15;
  potvar = potvar + 15;
  yourchipsvar = Number(yourchipsvar);
  potvar = Number(potvar);
  document.getElementById("pot").innerHTML = potvar;
  document.getElementById("yourchips").innerHTML = yourchipsvar;
}

function bet25() {

  yourchipsvar = yourchipsvar - 25;
  potvar = potvar + 25;
  yourchipsvar = Number(yourchipsvar);
  potvar = Number(potvar);
  document.getElementById("pot").innerHTML = potvar;
  document.getElementById("yourchips").innerHTML = yourchipsvar;
}

function bet50() {

  yourchipsvar = yourchipsvar - 50;
  potvar = potvar + 50;
  yourchipsvar = Number(yourchipsvar);
  potvar = Number(potvar);
  document.getElementById("pot").innerHTML = potvar;
  document.getElementById("yourchips").innerHTML = yourchipsvar;
}
var aceofspades = 0;
var twoofspades = 0;
var threeofspades = 0;
var fourofspades = 0;
var fiveofspades = 0;
var cardinhand = 0;

function deal() {
  if (whotodealto == 0) {
    dealuser();
  } else {
    alert("dealtotheAI");
  }
}

function dealuser() {
  var cardinhand = Math.floor(Math.random() * 6);

  if (cardinhand == 0) {
    if (aceofspades == 0) {
      alert("You got an ace of spades");
      var aceofspades = 1;
    }
  }

  if (cardinhand == 1) {
    if (twoofspades == 0) {
      alert("You got a two of spades");
      var twoofspades = 1;
    }
  }

  if (cardinhand == 2) {
    if (threeofspades == 0) {
      alert("You got a three of spades");
      var threeofspades = 1;
    }
  }

  if (cardinhand == 3) {
    if (fourofspades == 0) {
      alert("You got a four of spades");
      var fourofspades = 1;
    }
  }

  if (cardinhand == 4) {
    if (fiveofspades == 0) {
      alert("You got a five of spades");
      var fiveofspades = 1;
    }
  }
}

You are creating two different local variables for each aceofspades , twoofspades , etc. This is because var means to create a local variable in the scope you are in. So the variables defined here:

var aceofspades = 0;
var twoofspades = 0;
var threeofspades = 0;
var fourofspades = 0;
var fiveofspades = 0;
var cardinhand = 0;

function deal() {
    if(whotodealto==0){dealuser();}  else{alert("dealtotheAI");}}

function dealuser() {
    var cardinhand = Math.floor(Math.random()*6);

    ...
}

Are different from the ones you create here:

 function dealuser() {
    ...
    if(aceofspades==0){
        alert("You got an ace of spades"); 
        var aceofspades = 1;
    }

In that case var aceofspades means you are creating a local variable in the dealuser() function. Simply set the value of your already created variable, this way you are not making a new one:

if(aceofspades==0) {
    alert("You got an ace of spades"); 
    aceofspades = 1;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM