forked from gSchool/accumulator-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
534 lines (368 loc) · 13.4 KB
/
main.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
----------------------------------------
SAMPLE
----------------------------------------
Challenge: Write function named test that returns the string "This Works!".
Solution: This one has already been completed for you.
*/
function test() {
var string = "This Works!";
return string;
}
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write function named sum that will take an array of numbers and return the sum of them.
Example: if you pass it [1,2,3] then it should return 6 (which is 1 + 2 + 3)
NOTE: always look at the test results to see further details about what the test is expecting.
For example, the tests require that to complete this challenge, your function must return
0 if the input is empty. Please see the README file for an explanation of how to expand the test
results on the index page in the browser.
*/
const sum = (arr) => {
if (arr.length === 0) {
return 0;
}
return arr.reduce((total, current) => total + current);
};
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write function named doubleLetters that will take a string and double every letter in the string
Example: if you pass it "abc" then it should return "aabbcc"
*/
function doubleLetters(string) {}
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write function named doubleNumbers that will take an array of numbers and return an array with those numbers doubled
Example: if you pass it [1,2,3] then it should return [2,4,6]
*/
const doubleNumbers = (arr) => arr.map((num) => num * 2);
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write function named multiplyNumbers that will take an array of numbers and
return an array with those numbers multiplied by another value
Examples:
- if you call multiplyNumbers([1,2,3], 0) you'd get [0,0,0]
- if you call multiplyNumbers([1,2,3], 5) you'd get [5,10,15]
*/
const multiplyNumbers = (array, multiplier) => array.map((i) => i * multiplier);
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write function named interleave that will take two arrays and interleaves them
Example: if you pass it ["a", "b", "c"] and ["d", "e", "f"] then it should return ["a", "d", "b", "e", "c", "f"]
NOTE: you can assume each input will be the same length
*/
const interleave = (array1, array2) => {
let accum = [];
for (let i = 0; i < array1.length; i++) {
accum.push(array1[i]);
accum.push(array2[i]);
}
return accum;
};
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write function named createRange that will take a number and a default value and return an array of that many values
Example: if you pass it 4 and "Hello" then it should return ["Hello", "Hello", "Hello", "Hello"]
*/
const createRange = (length, element) => {
let arr = [];
for (let i = 0; i < length; i++) {
arr[i] = element;
}
return arr;
};
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write function named flipArray that will take an array and return an object where the keys are the items and the values are the indices
Example:
If you pass it ["quick", "brown", "fox"] then it should return { "quick": 0, "brown": 1, "fox": 2 }
*/
const flipArray = (arr) => {
let obj = {};
arr.forEach((element, index) => {
obj[element] = index;
});
return obj;
};
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write function named arraysToObject that will take an array of 2-element arrays, and return an object of key/value pairs
Example:
If you pass it [[2014, "Horse"], [2015, "Sheep"]] then it should return { 2014: "Horse", 2015: "Sheep" }
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write function named reverseString that will reverse a string without calling the built-in .split or .reverse methods
Example:
If you pass it "hello" then it should return "olleh"
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named repeats that returns true if the first half of the string equals the last half, and false if not
Example:
If you pass it "haha" then it should return true because "ha" (the first half) equals "ha" (the second half)
If you pass it "yay" then it should return false because it's odd
If you pass it "heehaw" then it should return false because "hee" doesn't equal "haw"
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named everyOther that returns every other character in the string
Example:
If you pass it "abcdef" then it should return "ace" because those represent every other letter
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named allEqual that returns true if every character in the string is the same
Example:
If you pass "aaa" it should return true
If you pass "aba" it should return false
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named sumLetters that returns the sum of every character in the string
Example:
If you pass "45" it should return 9
If you pass "246" it should return 12
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named countVowels that returns the number of vowels in a string, excluding "y"
Example:
If you pass "you" it should return 2
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named split that takes a string and returns an array of the letters
Example:
If you pass "you" it should return ["y", "o", "u"]
NOTE: do not use the builtin `split` method
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named getCodePoints that takes a string and returns an array of the codePoints of the letters
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt
Example:
If you pass "Hello" it should return [ 72, 101, 108, 108, 111 ]
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named letterMap that takes a string and returns an object of the letters and their positions in the string
Example:
If you pass "Yo" it should return {Y: 0, o: 1}
If you pass "Hello" it should return {H: 0, e: 1, l: 3, o: 4}
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named letterCount that takes a string and returns an object with the letters and the number of their occurrences
Example:
If you pass "Yo" it should return {Y: 1, o: 1}
If you pass "Hello" it should return {"H": 1, "e": 1, "l": 2, "o": 1}
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named threeOdds that takes 2 numbers and returns true if there are 3 odd numbers _between_ those two numbers
Example:
If you pass 0,3 it should return false because the only number between 0 and 3 is 1
If you pass 0,6 it should return true because between 0 and six (the numbers 1,2,3,4,5) there are three odds - 1, 3 and 5
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function called leftPad that takes a string, a length and a fill character and returns a string padded to length with the fill character
Example:
If you pass "a", 3, "*" it should return "**a" - that is, a string of length 3, padded on the left by the "*" character
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named createString that takes a number and a letter and creates a string of that many letters
Example:
If you pass "a", 3 it should return "aaa"
If you pass "b", 3 it should return "bb"
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named factorial that takes a number and returns its factorial
Factorial of 4 means 4 * 3 * 2 * 1
Example:
If you pass 4 it should return 24 since that's 4 * 3 * 2 * 1
If you pass 5 it should return 120 since that's 5 * 4 * 3 * 2 * 1
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named arrayOfNumbers that takes a number and returns an array of all the numbers between 1 and that number, inclusive
Example:
If you pass 1 it should return [1]
If you pass 3 it should return [1,2,3]
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named evenOdd that takes a number and returns an object with the numbers and whether they are even or odd
Example:
If you pass 1,4 it should return {"1": "odd", "2": "even", "3": "odd", "4": "even"}
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named growingKeys that takes a number and a string and returns an object where the keys are that string, repeated one more each time
Example:
If you pass 2,"d" it should return {"d": true, "dd": true}
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named every that takes an array and a value and returns true if every element of the array equals the value
Example:
If you pass [1,1], 1 it should return true
If you pass [1,2], 1 it should return false
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named some that takes an array and a value and returns true if at least one element of the array equals the value
Example:
If you pass [1,2], 1 it should return true
If you pass [3,2], 1 it should return false
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named some that takes an array and returns a string with the elements joined by commas, with a trailing 'and'
Example:
If you pass ["Sue", "Will"] it should return "Sue and Will"
If you pass ["Sue", "Will", "Rachel"] it should return "Sue, Will and Rachel"
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named acronym that takes an array and returns a string that is an acronym of the items in the array
Example:
If you pass ["Sue", "Will"] it should return "SW"
If you pass ["Java", Script", "Object", "Notation"] it should return "JSON"
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named min that takes an array and returns minimum value of the array
Example:
If you pass [0,-3,2,5] it should return -3
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named index that takes an array of objects, and a property name, and returns an object where the keys are the specified property
Example:
If you pass [{id: 1, name: "Joe"}, {id: 2, name: "Sue"}] it should return {1: {id: 1, name: "Joe"}, 2: {id: 2, name: "Sue"}}
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named invert that takes an object and returns an object where the keys and values have been inverted
Example:
If you pass {id: 1, name: "Joe"} it should return {1: "id", Joe: "name"}
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named addSignature that takes an object and a name, and returns an object where
- the keys are suffixed with "-signed"
- the values are suffixed with " - <name>"
Example:
If you pass {"contract": "foo"}, "Fred" it should return {"contract-signed": "foo - Fred"}
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named pairs that takes an object and returns an array of strings of key/value pairs
Example:
If you pass {name: "Will", age: 24} it should return ["name - will", "age - 24"]
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named sumValues that takes an object and returns the sum of the values
Example:
If you pass {a: 1, b: 2} it should return 3
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named biggestProperty that takes an object and returns the name of the property with the highest value
Example:
If you pass {1999: 4036, 2000: 7654} it should return '2000'
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named keyForValue that takes an object and a value and returns the name of the property that matches that value
Example:
If you pass {1999: 4036, 2000: 7654} and 4036, it should return '1999'
*/
/*
----------------------------------------
CHALLENGE
----------------------------------------
Write a function named containsValue that takes an object and a value and returns true if the object contains the value
Example:
If you pass {1999: 4036, 2000: 7654} and 4036, it should return true
*/
//