-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgolf-score-translator.js
More file actions
122 lines (118 loc) · 3.12 KB
/
Copy pathgolf-score-translator.js
File metadata and controls
122 lines (118 loc) · 3.12 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* -----------------------------------------------------------------------------
* Golf Score Calculator
* -----------------------------------------------------------------------------
*
* In golf, every hole has a **par**, which represents the expected number of
* strokes needed to complete the hole.
*
* The player's score is determined by comparing the number of **strokes** to
* the **par**. Depending on the result, a specific golf term is returned.
*
* -----------------------------------------------------------------------------
* Objective
* -----------------------------------------------------------------------------
*
* Implement a function named `golfScore(par, strokes)` that returns the
* appropriate golf score nickname.
*
* -----------------------------------------------------------------------------
* Function Signature
* -----------------------------------------------------------------------------
*
* golfScore(par, strokes) -> string
*
* Parameters:
* - par: Number
* - strokes: Number
*
* Returns:
* - String
*
* -----------------------------------------------------------------------------
* Scoring Rules
* -----------------------------------------------------------------------------
*
* Hole-in-one!
* - strokes === 1
*
* Eagle
* - strokes === par - 2
*
* Birdie
* - strokes === par - 1
*
* Par
* - strokes === par
*
* Bogey
* - strokes === par + 1
*
* Double Bogey
* - strokes === par + 2
*
* Go Home!
* - strokes >= par + 4
*
* -----------------------------------------------------------------------------
* Test Cases
* -----------------------------------------------------------------------------
*
* Hole-in-one!
* golfScore(1, 1) -> "Hole-in-one!"
* golfScore(3, 1) -> "Hole-in-one!"
* golfScore(4, 1) -> "Hole-in-one!"
* golfScore(5, 1) -> "Hole-in-one!"
*
* Eagle
* golfScore(4, 2) -> "Eagle"
* golfScore(5, 2) -> "Eagle"
*
* Birdie
* golfScore(3, 2) -> "Birdie"
* golfScore(4, 3) -> "Birdie"
* golfScore(5, 4) -> "Birdie"
*
* Par
* golfScore(3, 3) -> "Par"
* golfScore(4, 4) -> "Par"
* golfScore(5, 5) -> "Par"
*
* Bogey
* golfScore(3, 4) -> "Bogey"
* golfScore(4, 5) -> "Bogey"
* golfScore(5, 6) -> "Bogey"
*
* Double Bogey
* golfScore(3, 5) -> "Double Bogey"
* golfScore(4, 6) -> "Double Bogey"
* golfScore(5, 7) -> "Double Bogey"
*
* Go Home!
* golfScore(3, 7) -> "Go Home!"
* golfScore(4, 8) -> "Go Home!"
* golfScore(5, 9) -> "Go Home!"
*
* -----------------------------------------------------------------------------
* Time Complexity: O(1)
* Space Complexity: O(1)
* -----------------------------------------------------------------------------
*/
function golfScore(par,strokes){
if(strokes == 1){
return "Hole-in-one!"
} else if(strokes <= par - 2){
return "Eagle"
}else if(strokes == par - 1){
return "Birdie"
} else if(strokes == par){
return "Par"
}else if(strokes == par + 1){
return "Bogey"
} else if(strokes == par + 2){
return "Double Bogey"
} else if(strokes >= par + 3){
return "Go Home!"
}
}
console.log(golfScore(3, 7))