-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
55 lines (47 loc) · 1.55 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
let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;
// Write your code below:
function generateTarget() {
return Math.floor(Math.random() * 10);
}
function getAbsoluteDistance(guess, targetGuess) {
return Math.abs(targetGuess - guess);
}
function compareGuesses(humanGuess, computerGuess, targetGuess) {
if ((humanGuess < 0) || (humanGuess > 9)) {
alert('Please enter a number between 0 and 9');
} else {
let humanDistance = getAbsoluteDistance(humanGuess, targetGuess);
let computerDistance = getAbsoluteDistance(computerGuess, targetGuess);
if (humanDistance < computerDistance) {
return true;
} else if (humanDistance > computerDistance) {
return false;
} else if (humanDistance === computerDistance) {
return true;
}
}
}
// function compareGuesses(humanGuess, computerGuess, targetGuess) {
// if (Math.abs(targetGuess - humanGuess) < Math.abs(targetGuess - computerGuess)) {
// return true;
// } else if (Math.abs(targetGuess - humanGuess) > Math.abs(targetGuess - computerGuess)) {
// return false;
// } else if (Math.abs(targetGuess - humanGuess) === Math.abs(targetGuess - computerGuess)) {
// return true;
// }
// }
function updateScore(winner) {
if (winner === 'human') {
humanScore += 1;
} else if (winner === 'computer') {
computerScore += 1;
} else {
humanScore += 0;
computerScore += 0;
}
}
function advanceRound() {
currentRoundNumber += 1;
}