javascript nested if else code example
Example 1: nested if javascript
if(a == 'value'){
doSomething();
if(b == 'another value'){
doAnotherThing();
}
}
Example 2: js nested if else
var word = ['rock', 'paper', 'scissors'];
var player1 = word[Math.floor(Math.random() * word.length)];
var player2 = word[Math.floor(Math.random() * word.length)];
var player1Image = document.querySelectorAll("img")[0].setAttribute("src", "images/" + player1 + ".png");
var player2Image = document.querySelectorAll("img")[1].setAttribute("src", "images/" + player2 + ".png");
if(player1 === "rock"){
if(player2 === "scissors"){
player1Image;
player2Image;
console.log(player1 + " beats " + player2 + ", player1 wins");
} else if(player2 === "paper"){
player1Image;
player2Image;
console.log(player2 + " beats " + player1 + ", player2 wins");
} else {
player1Image;
player2Image;
console.log("Draw!!!")
}
} else if(player1 === "scissors"){
if(player2 === "rock"){
player1Image;
player2Image;
console.log(player2 + " beats " + player1 + ", player2 wins");
} else if(player2 === "paper"){
player1Image;
player2Image;
console.log(player1 + " beats " + player2 + ", player1 wins");
} else {
player1Image;
player2Image;
console.log("Draw!!!");
}
} else {
if(player1 === "paper"){
if(player2 === "rock"){
player1Image;
player2Image;
console.log(player1 + " beats " + player2 + ", player1 wins");
} else if(player2 === "scissors"){
player1Image;
player2Image;
console.log(player2 + " beats " + player1 + ", player2 wins");
} else {
player1Image;
player2Image;
console.log("Draw!!!");
}
}
}