-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
107 lines (82 loc) · 2.35 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// just for testing for link between js file and html file
// let x ='testing'
// alert(x);
var rock = "rock";
var paper = "paper";
var scissors = "scissors";
var computerWeapons = [rock, paper, scissors];
// ### function that creates random numbers from array elements ###
function selectElement(element) {
const random = Math.floor(Math.random() * computerWeapons.length);
// alert(random);
// ### placing function within a function ###
const convertRandomToString = function (random) {
// ### convert integer to string ###
switch (random) {
case 0 :
return rock;
break;
case 1 :
return paper;
break;
case 2 :
return scissors;
break;
default :
}
}
// ### another way of anonymous function within a function but using the arrow function instead ###
// const convertRandomToString = (random) => {
// convert integer to string
// switch (random) {
// case 0 :
// return rock;
// break;
// case 1 :
// return paper;
// break;
// case 2 :
// return scissors;
// break;
// default :
// }
// }
// ### Accessing newly converted random strings from array and assigning it to a new variable ###
const computerPlayer = computerWeapons[random];
// OR us below
// const computerPlayer = convertRandomToString(random);
// ### assigning each parameter element the corresponding logic &/or conditionals
if (element === rock) {
// alert("rock");
if (computerPlayer === rock) {
alert("it's a tie");
} else if (computerPlayer === paper) {
alert("computer wins");
} else if (computerPlayer === scissors) {
alert("player wins");
}
return;
};
if (element === paper) {
// alert("paper");
if (computerPlayer === rock) {
alert("player wins");
} else if (computerPlayer === paper) {
alert("it's a tie");
} else if (computerPlayer === scissors) {
alert("computer wins");
}
return;
}
if (element === scissors) {
// alert("scissors");
if (computerPlayer === rock) {
alert("computer wins");
} else if (computerPlayer === paper) {
alert("player wins");
} else if (computerPlayer === scissors) {
alert("it's a tie");
}
return;
}
}