-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
72 lines (64 loc) · 2.36 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
function getComputerChoice(){
let computerChoice
let computerChoiceNumber = Math.random()
if(computerChoiceNumber <= 0.3 && computerChoiceNumber >0){
computerChoice = "rock"
} else if(computerChoiceNumber <= 0.6 && computerChoiceNumber >0.3) {
computerChoice = "paper"
} else if(computerChoiceNumber <= 1 && computerChoiceNumber >0.6){
computerChoice = "scissors"
}
return computerChoice
}
function getHumanChoice(){
let userInput = prompt("Enter you choice rock, paper, scissor \n Note: answer is case sensitive", "Now your opponent will also decide")
return userInput
}
function playGame(){
function playRound(humanChoice,computerChoice){
let pick = humanChoice.toLowerCase()
console.log(pick)
console.log(computerChoice)
if(pick === computerChoice){
console.log("It's a tie")
} else if(pick === "paper"){
console.log(`Your Choice ${pick}`)
console.log(`Computer Choice ${computerChoice}`)
if(computerChoice === "scissors"){
console.log("Computer Wins")
computerScore++
} else {
console.log("You Win")
humanScore++
}
} else if(pick === "rock"){
console.log(`Your Choice ${pick}`)
console.log(`Computer Choice ${computerChoice}`)
if(computerChoice === "paper"){
console.log("Computer Wins")
computerScore++
} else {
console.log("You Win")
humanScore++
}
} else if(pick === "scissors"){
console.log(`Your Choice ${pick}`)
console.log(`Computer Choice ${computerChoice}`)
if(computerChoice === "rock"){
console.log("Computer Wins")
computerScore++
} else {
console.log("You Win")
humanScore++
}
}
console.log(`Scores: \n Your Score: ${humanScore} \n Computer Score: ${computerScore}`)
}
let humanScore = 0
let computerScore = 0
for(let i = 0; i < 5 ; i++){
playRound(getHumanChoice(),getComputerChoice())
}
console.log(humanScore > computerScore ? "You Win" : "Computer Wins")
}
playGame()