forked from jsd20191008/hw-01-functions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pset-functions.js
310 lines (195 loc) · 6.26 KB
/
pset-functions.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/***********
Problem: Blackjack
Create a function named `blackJack()` that accepts two parameters:
playerCardScore (number)
dealerCardScore (number)
Return whichever value is nearest (or equal) to 21 without going over. Return 0 if both scores go over 21.
Test Cases:
Use the following test cases to confirm your program meets the success criteria
1. blackJack(19, 21)
Result: 21
2. blackJack(22, 22)
Result: 0
3. blackJack(19, 22)
Result: 19
3. blackJack(21, 21)
Result: 21
************/
console.log('Problem 1:')
// Add your code below this line
// Add your code above this line
function blackJack(playerCardScore, dealerCardScore) {
const MAX = 21;
if(playerCardScore > MAX && dealerCardScore > MAX){
return 0;
} else {
if(playerCardScore >= playerCardScore && dealerCardScore <= MAX || dealerCardScore > MAX){
return playerCardScore;
} else {
return dealerCardScore;
}
}
}
const result = blackJack(21, 21)
console.log (result)
/** added for formatting purposes **/
console.log('')
console.log('-----------------')
/***********
Problem: Word Counter
Create a function named `wordCount()` that accepts a parameter called "phrase" which counts the number of occurrences of each word in that phrase
For example, the phrase: "olly olly in come free" should result in output that looks similar to the following:
```
olly: 1
in: 1
come: 1
free: 1
```
Feel free to add any additional functions or variables you deem necessary to meet the above requirements
Test Cases:
Use the following test cases to confirm your program meets the success criteria
1. wordCount("olly olly in come free")
Result:
olly: 2
in: 1
come: 1
free: 1
2. wordCount("Baby shark, doo doo doo doo doo doo")
Result:
baby: 1
shark: 1
doo: 6
3. wordCount("Humpty Dumpty sat on a wall Humpty Dumpty had a great fall")
Result:
humpty: 2
dumpty: 2
sat: 1
a: 2
on: 1
fall: 1
great: 1
had: 1
wall: 1
************/
console.log('Problem 2:')
// Add your code below this line
function wordCount(phrase){
let obj={};
phrase.split(" ").forEach(function(el,i,arr){
obj[el]= obj[el]? ++obj[el]: 1;
});
return obj;
}
console.log(wordCount("olly olly in come free"));
// Add your code above this line
/** added for formatting purposes **/
console.log('')
console.log('-----------------')
/***********
Problem: Scrabble Scorer
Create a function named `scrabbleScore()` that accepts a parameter called "word". The function should use the following table to calculate the Scrabble score of a provided word:
```
Letter Value
A, E, I, O, U, L, N, R, S, T 1
D, G 2
B, C, M, P 3
F, H, V, W, Y 4
K 5
J, X 8
Q, Z 10
```
Feel free to add any additional functions or variables you deem necessary to meet the above requirements
Test Cases:
Use the following test cases to confirm your program meets the success criteria
1. scrabbleScore("cabbage")
Note: c = 3, a = 1 (twice), b = 3 (twice), g = 2, e = 1
Expected Result: 14
2. scrabbleScore("javascript")
Expected Result: 24
3. scrabbleScore("function")
Expected Result: 13
************/
console.log('Problem 3:')
// Add your code below this line
const word = "function"
const scoreKey = {a: 1, b: 3, c: 3, d: 2, e: 1, f: 4, g: 2, h: 4, i: 1, j: 8, k: 5, l: 1, m: 3, n: 1, o: 1, p: 3, q: 10, r: 1, s: 1, t: 1, u: 1, v: 4, w: 4, x: 8, y: 4, z: 10}
function scrabbleScore(word) {
const wordArray = word.split ('')
let score = 0
wordArray.forEach(letter => {
score = score + scoreKey[letter]
});
return score
}
console.log(scrabbleScore("function"))
}
const sum = [...word].reduce((accu, letter) => { return accu + scrabble[letter.toLowerCase()]; }, 0);
}
return results
}
console.log(scrabbleScore("cabbage"))
// Add your code above this line
/** added for formatting purposes **/
console.log('')
console.log('-----------------')
/***********
Problem: Palindromes
Palindromes are words which read the same forward as backwards. For example, "madam" and "noon" are both palindromes.
Create a function named `isPalindrome()` that accepts a parameter called "word". The function will return true if the word is a palindrome and false if it is not.
Feel free to add any additional functions or variables you deem necessary to meet the above requirements
Test Cases:
Use the following test cases to confirm your program meets the success criteria
1. isPalindrome("noon")
Expected Result: true
2. isPalindrome("racecar")
Expected Result: true
3. isPalindrome("moon")
Expected Result: false
4. isPalindrome("run")
Expected Result: false
************/
console.log('Problem 4:')
// Add your code below this line
// Add your code above this line
/** added for formatting purposes **/
console.log('')
console.log('-----------------')
/***********
Problem: Double Letter Checker
Create a function that takes a "word" as a parameter and returns true if the word has two consecutive identical letters.
Feel free to add any additional functions or variables you deem necessary to meet the above requirements
Test Cases:
Use the following test cases to confirm your program meets the success criteria
1. doubleLetters("loop")
Expected Result: true
2. doubleLetters("rune")
Expected Result: false
3. doubleLetters("apple")
Expected Result: true
************/
console.log('Problem 5:')
// Add your code below this line
function doubleLetters(word) {
// split the word into an array of Letters
const letters = word.split('')
console.log(letters)
// loop through array of doubleLetters
//check if each letter is the same as the next letter
// in the sequence
let results = false
letters.forEach((letter, index) => {
// check if current letter is the same as the next letter
console.log(`letter: ${letter}`)
console.log(`index: ${index}`)
if (letter === letters[index + 1]) {
conole.log('we have a match')
result = true
}
})
return result
}
console.log(doubleLetters('rune'))
// Add your code above this line
/** added for formatting purposes **/
console.log('')
console.log('-----------------')