better way to do nested if statements javascipt

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"){ // 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"){ // 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 { // paper 
                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!!!");
                    }
                }
        }

4.5
8
Awgiedawgie 440220 points

                                    /_ return early when invalid conditions found _/

function test(fruit, quantity) {
  const redFruits = ['apple', 'strawberry', 'cherry', 'cranberries'];

  if (!fruit) throw new Error('No fruit!'); // condition 1: throw error early
  if (!redFruits.includes(fruit)) return; // condition 2: stop when fruit is not red

  console.log('red');

  // condition 3: must be big quantity
  if (quantity > 10) {
    console.log('big quantity');
  }
}

4.5 (8 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
better way to do nested if statements javascipt nesting if…else statement js if nested javascript simplest way to write nested if else in javascript nested if javascript nested if statement in javascript best way to treat nested if else javacsript nested if statement js can i nest another if inside an if statement in javascript who use of if nested in javascript nested else if in javascript js nested if else if nested if else statement in javascript javascript nested else condition not work nested else condition not work in javascript what is nested if in javascript javascript nested lopp and conditional statements nested if statements js nested if then javscript "?" nested if else statement in javascript with 3 different variables conditions with nested loop javascript nested loop and if else in javascript nested if else loop in javascript nesting if statements in else if javascript can you nest if statements in javascript js nesting if statement nested if js can you nest if statements in eachother js nesting if statements with if statements javascript nested if in javasciprT javascript nested if else if if else nested if in javascript nested if or if/else in js nested if else js js nested if else javascript how to simplify nested if statement nested if else javascript javascript nested if else example NESTED IF IN JS nested if statements javascript good pratices nested if statements javascript javascript nested statements nested if in js nested if else into conditonal operators js nested if condition in js nested if else in js nested if condition in javascript how to use nested if in javascript nested if statement javascript nested if conditions javascript how to do a nested if in javascript nested if else in javascript if else nested javascript javascript nested if statements nested if in javascript how to do nested if else in liner javascript using multiple if statements within a while loop javascript nested if statements how can i write two if statement together in js javascript if else nested how to nest if statements inside function in js example of nested if statements javascript example of nested conditional statements javascript how to have nested if statements in js loops in nested if statements javascript example of multiple if statements in JavaScript easier way to nest if statements in javascript nested if statement rules js nested or if javascript nested if statement inside for loop javascript if statement nested in an else javascript cascading if statement javascript multi if else check javascript java script if loop nested in for javascript for loop nested if i j java script nesting conditionals nested if making and javascript can you have a nested if statement in javascript nesting statements in javascript nested conditional statements javascript nesting if else statement js if else nested statements js how to use if inside an if statement javascript nesting if statements javascript how to write multiple if or statemetns js is it bad to have nested if statements in js how to combine multiple if else statement in javascript
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source