This repository was archived by the owner on Nov 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspel.js
More file actions
52 lines (46 loc) · 1.37 KB
/
Copy pathspel.js
File metadata and controls
52 lines (46 loc) · 1.37 KB
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
function userChoice () {
return prompt('Vill du välja, sten, sax eller påse?');
}
function computerChoice () {
var randomNumber = Math.random();
if (randomNumber <= 0.34) {
return 'sten';
} else if (randomNumber <= 0.67) {
return 'påse';
} else {
return 'sax';
}
}
function compare (human, computer) {
this.human = human;
this.computer = computer;
if (human === computer) {
return 'Det är lika, ingen av er vinner';
} else if (human === 'sten') {
if (computer === 'sax') {
return message (human, computer, true);
} else {
return message (human, computer, false);
}
} else if (human === 'sax') {
if (computer === 'sten') {
return message (human, computer, false);
} else {
return message (human, computer, true);
}
} else if (human === 'påse') {
if (computer === 'sten') {
return message (human, computer, true);
} else {
return message (human, computer, false);
}
}
}
function message (human, computer, humanIsWinner) {
if (humanIsWinner === true) {
return 'Du valde ' + human + ', datorn valde ' + computer + ', du vinner';
} else {
return 'Du valde ' + human + ', datorn valde ' + computer + ', du förlorar';
}
}
alert(compare(userChoice(), computerChoice()));