1if(a == 'value'){
2 doSomething();
3 if(b == 'another value'){
4 doAnotherThing();
5 }
6}
1var word = ['rock', 'paper', 'scissors'];
2
3 var player1 = word[Math.floor(Math.random() * word.length)];
4 var player2 = word[Math.floor(Math.random() * word.length)];
5
6 var player1Image = document.querySelectorAll("img")[0].setAttribute("src", "images/" + player1 + ".png");
7 var player2Image = document.querySelectorAll("img")[1].setAttribute("src", "images/" + player2 + ".png");
8
9 if(player1 === "rock"){ // rock
10 if(player2 === "scissors"){
11 player1Image;
12 player2Image;
13
14 console.log(player1 + " beats " + player2 + ", player1 wins");
15 } else if(player2 === "paper"){
16 player1Image;
17 player2Image;
18 console.log(player2 + " beats " + player1 + ", player2 wins");
19 } else {
20 player1Image;
21 player2Image;
22 console.log("Draw!!!")
23 }
24
25 } else if(player1 === "scissors"){ // scissors
26 if(player2 === "rock"){
27 player1Image;
28 player2Image;
29 console.log(player2 + " beats " + player1 + ", player2 wins");
30 } else if(player2 === "paper"){
31 player1Image;
32 player2Image;
33 console.log(player1 + " beats " + player2 + ", player1 wins");
34 } else {
35 player1Image;
36 player2Image;
37 console.log("Draw!!!");
38 }
39
40
41 } else { // paper
42 if(player1 === "paper"){
43 if(player2 === "rock"){
44 player1Image;
45 player2Image;
46 console.log(player1 + " beats " + player2 + ", player1 wins");
47 } else if(player2 === "scissors"){
48 player1Image;
49 player2Image;
50 console.log(player2 + " beats " + player1 + ", player2 wins");
51 } else {
52 player1Image;
53 player2Image;
54 console.log("Draw!!!");
55 }
56 }
57 }
1/_ return early when invalid conditions found _/
2
3function test(fruit, quantity) {
4 const redFruits = ['apple', 'strawberry', 'cherry', 'cranberries'];
5
6 if (!fruit) throw new Error('No fruit!'); // condition 1: throw error early
7 if (!redFruits.includes(fruit)) return; // condition 2: stop when fruit is not red
8
9 console.log('red');
10
11 // condition 3: must be big quantity
12 if (quantity > 10) {
13 console.log('big quantity');
14 }
15}