-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunderpants.js
More file actions
630 lines (518 loc) · 15.3 KB
/
underpants.js
File metadata and controls
630 lines (518 loc) · 15.3 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
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
// This makes the arguments variable behave the way we want it to and a few
// other things. For more info:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
'use strict';
var _ = {};
/**
* START OF OUR LIBRARY!
* Implement each function below its instructions
*/
/** _.identity
* Arguments:
* 1) Any value
* Objectives:
* 1) Returns <value> unchanged
* Examples:
* _.identity(5) === 5
* _.identity({a: "b"}) === {a: "b"}
*/
_.identity = function (value) {
return value
}
/** _.typeOf
* Arguments:
* 1) Any value
* Objectives:
* 1) Return the type of <value> as a string
* Types are one of:
* - "string"
* - "array"
* - "object"
* - "undefined"
* - "number"
* - "boolean"
* - "null"
* - "function"
* Examples:
* _.typeOf(134) -> "number"
* _.typeOf("javascript") -> "string"
* _.typeOf([1,2,3]) -> "array"
*/
_.typeOf = function (value) {
if (value === null) {
return 'null'
}
if (typeof value === 'string') {
return "string"
}
if (typeof value === 'number') {
return "number"
}
if (Array.isArray(value)) {
return "array";
}
if (typeof value === 'object') {
return "object"
}
if (typeof value === 'undefined') {
return "undefined"
}
if (typeof value === 'boolean') {
return "boolean"
}
if (typeof value === 'function') {
return "function"
}
}
/** _.first
* Arguments:
* 1) An array
* 2) A number
* Objectives:
* 1) If <array> is not an array, return []
* 2) If <number> is not given or not a number, return just the first element in <array>.
* 3) Otherwise, return the first <number> items of <array>
* Edge Cases:
* 1) What if <number> is negative?
* 2) What if <number> is greater than <array>.length?
* Examples:
* _.first("ponies", 1) -> []
* _.first(["a", "b", "c"], "ponies") -> "a"
* _.first(["a", "b", "c"], 1) -> "a"
* _.first(["a", "b", "c"], 2) -> ["a", "b"]
*/
_.first = function (array, value) {
let result = []
if (typeof value !== 'number') {
return array[0]
}
if (value > array.length) {
return array
}
if (value < 0) {
return result
}
if (value === array.length - 1) {
return array.slice(0, value)
}
if (Array.isArray !== array) {
return result
}
}
/** _.last
* Arguments:
* 1) An array
* 2) A number
* Objectives:
* 1) If <array> is not an array, return []
* 2) If <number> is not given or not a number, return just the last element in <array>.
* 3) Otherwise, return the last <number> items of <array>
* Edge Cases:
* 1) What if <number> is negative?
* 2) What if <number> is greater than <array>.length?
* Examples:
* _.last("ponies", 2) -> []
* _.last(["a", "b", "c"], "ponies") -> "c"
* _.last(["a", "b", "c"], 1) -> "c"
* _.last(["a", "b", "c"], 2) -> ["b", "c"]
*/
_.last = function (array, number) {
if (typeof number !== 'number') {
return array.pop()
}
if (number < 0) {
return []
}
if (number === array.length - 1) {
return array.slice(-number, 3)
}
if (number > array.length) {
return array
}
if (Array.isArray !== array) {
return []
}
}
/** _.indexOf
* Arguments:
* 1) An array
* 2) A value
* Objectives:
* 1) Return the index of <array> that is the first occurrance of <value>
* 2) Return -1 if <value> is not in <array>
* 3) Do not use [].indexOf()!
* Edge Cases:
* 1) What if <array> has multiple occurances of val?
* 2) What if <val> isn't in <array>?
* Examples:
* _.indexOf(["a","b","c"], "c") -> 2
* _.indexOf(["a","b","c"], "d") -> -1
*/
_.indexOf = function (array, value) {
for (let i = 0; i < array.length; i++) {
if (array[i] === value) {
return i
}
}
return -1
}
/** _.contains
* Arguments:
* 1) An array
* 2) A value
* Objectives:
* 1) Return true if <array> contains <value>
* 2) Return false otherwise
* 3) You must use the ternary operator in your implementation.
* Edge Cases:
* 1) did you use === ?
* 2) what if no <value> is given?
* Examples:
* _.contains([1,"two", 3.14], "two") -> true
*/
_.contains = function (array, value) {
for (let i = 0; i < array.length; i++) {
if (array[i] === value) {
return true
}
}
for (let i = 0; i < array.length; i++) {
if (array[i] !== value) {
return false
}
}
}
/** _.each
* Arguments:
* 1) A collection
* 2) A function
* Objectives:
* 1) if <collection> is an array, call <function> once for each element
* with the arguments:
* the element, it's index, <collection>
* 2) if <collection> is an object, call <function> once for each property
* with the arguments:
* the property's value, it's key, <collection>
* Examples:
* _.each(["a","b","c"], function(e,i,a){ console.log(e)});
* -> should log "a" "b" "c" to the console
*/
_.each = function (collection, func) {
if (Array.isArray === collection) {
for (let i = 0; i < collection.length; i++) {
func(collection[i], i, collection)
}
}
else if (typeof collection === "object") {
for (let key in collection) {
func(collection[key], key, collection)
}
}
}
/** _.unique
* Arguments:
* 1) An array
* Objectives:
* 1) Return a new array of all elements from <array> with duplicates removed
* 2) Use _.indexOf() from above
* Examples:
* _.unique([1,2,2,4,5,6,5,2]) -> [1,2,4,5,6]
*/
_.unique = function (array) {
var temparray = []
for (let i = 0; i < array.length; i++) {
if (temparray.indexOf(array[i]) === -1) {
temparray.push(array[i]);
}
}
return temparray;
}
/** _.filter
* Arguments:
* 1) An array
* 2) A function
* Objectives:
* 1) call <function> for each element in <array> passing the arguments:
* the element, it's index, <array>
* 2) return a new array of elements for which calling <function> returned true
* Edge Cases:
* 1) What if <function> returns something other than true or false?
* Examples:
* _.filter([1,2,3,4,5], function(x){return x%2 === 0}) -> [2,4]
* Extra Credit:
* use _.each in your implementation
*/
_.filter = function (array, func) {
let newArray = []
for (let i = 0; i < array.length; i++) {
let result = func(array[i], i, array);
if (result === true) {
newArray.push(array[i]);
}
}
return newArray
}
/** _.reject
* Arguments:
* 1) An array
* 2) A function
* Objectives:
* 1) call <function> for each element in <array> passing the arguments:
* the element, it's index, <array>
* 2) return a new array of elements for which calling <function> returned false
* 3) This is the logical inverse if _.filter()
* Examples:
* _.reject([1,2,3,4,5], function(e){return e%2 === 0}) -> [1,3,5]
*/
_.reject = function (array, func) {
let newArray = []
for (let i = 0; i < array.length; i++) {
let result = func(array[i], i, array);
if (result === false) {
newArray.push(array[i])
}
}
return newArray
}
/** _.partition
* Arguments:
* 1) An array
* 2) A function
* Objectives:
* 1) Call <function> for each element in <array> passing it the arguments:
* element, key, <array>
* 2) Return an array that is made up of 2 sub arrays:
* 0) An array that contains all the values for which <function> returned something truthy
* 1) An array that contains all the values for which <function> returned something falsy
* Edge Cases:
* 1) This is going to return an array of arrays.
* Examples:
* _.partition([1,2,3,4,5], function(element,index,arr){
* return element % 2 === 0;
* }); -> [[2,4],[1,3,5]]
}
*/
_.partition = function (array, func) {
let newArray = [[], []]
for (let i = 0; i < array.length; i++) {
let result = func(array[i], i, array);
if (result === true) {
newArray[0].push(array[i])
}
if (result === false) {
newArray[1].push(array[i])
}
}
return newArray
}
/** _.map
* Arguments:
* 1) A collection
* 2) a function
* Objectives:
* 1) call <function> for each element in <collection> passing the arguments:
* if <collection> is an array:
* the element, it's index, <collection>
* if <collection> is an object:
* the value, it's key, <collection>
* 2) save the return value of each <function> call in a new array
* 3) return the new array
* Examples:
* _.map([1,2,3,4], function(e){return e * 2}) -> [2,4,6,8]
*/
_.map = function (collection, func) {
let newArray = []
if (!Array.isArray(collection)) {
for (let key in collection) {
let result = func(collection[key], key, collection)
newArray.push(result)
}
}
for (let i = 0; i < collection.length; i++) {
if (Array.isArray(collection)) {
let result = func(collection[i], i, collection)
newArray.push(result)
}
}
return newArray
}
/** _.pluck
* Arguments:
* 1) An array of objects
* 2) A property
* Objectives:
* 1) Return an array containing the value of <property> for every element in <array>
* 2) You must use _.map() in your implementation.
* Examples:
* _.pluck([{a: "one"}, {a: "two"}], "a") -> ["one", "two"]
*/
_.pluck = function (array, property) {
let newArray = []
for (let i = 0; i < array.length; i++) {
newArray.push(array[i][property])
}
return newArray
}
/** _.every
* Arguments:
* 1) A collection
* 2) A function
* Objectives:
* 1) Call <function> for every element of <collection> with the paramaters:
* if <collection> is an array:
* current element, it's index, <collection>
* if <collection> is an object:
* current value, current key, <collection>
* 2) If the return value of calling <function> for every element is true, return true
* 3) If even one of them returns false, return false
* 4) If <function> is not provided, return true if every element is truthy, otherwise return false
* Edge Cases:
* 1) what if <function> doesn't return a boolean
* 2) What if <function> is not given?
* Examples:
* _.every([2,4,6], function(e){return e % 2 === 0}) -> true
* _.every([1,2,3], function(e){return e % 2 === 0}) -> false
*/
_.every = function (collection, func) {
let value = true; // Initialize to true, assuming all elements pass the condition
if (typeof func !== 'function') {
for (let i = 0; i < collection.length; i++) {
if (!collection[i]) {
return false
}
}
return true
}
if (Array.isArray(collection)) {
for (let i = 0; i < collection.length; i++) {
let result = func(collection[i], i, collection);
if (!result) {
value = false; // If any result is false, update value and break the loop
break;
}
}
} else {
for (let key in collection) {
let result = func(collection[key], key, collection);
if (!result) {
value = false; // If any result is false, update value and break the loop
break;
}
}
}
return value;
};
/** _.some
* Arguments:
* 1) A collection
* 2) A function
* Objectives:
* 1) Call <function> for every element of <collection> with the paramaters:
* if <collection> is an array:
* current element, it's index, <collection>
* if <collection> is an object:
* current value, current key, <collection>
* 2) If the return value of calling <function> is true for at least one element, return true
* 3) If it is false for all elements, return false
* 4) If <function> is not provided return true if at least one element is truthy, otherwise return false
* Edge Cases:
* 1) what if <function> doesn't return a boolean
* 2) What if <function> is not given?
* Examples:
* _.some([1,3,5], function(e){return e % 2 === 0}) -> false
* _.some([1,2,3], function(e){return e % 2 === 0}) -> true
*/
_.some = function (collection, func) {
let value = false; // Initialize to true, assuming all elements pass the condition
if (typeof func !== 'function') {
for (let i = 0; i < collection.length; i++) {
if (!collection[i]) {
return false
}
}
return true
}
if (Array.isArray(collection)) {
for (let i = 0; i < collection.length; i++) {
let result = func(collection[i], i, collection);
if (result) {
value = true; // If any result is false, update value and break the loop
break;
}
}
} else {
for (let key in collection) {
let result = func(collection[key], key, collection);
if (result) {
value = true; // If any result is false, update value and break the loop
break;
}
}
}
return value;
}
/** _.reduce
* Arguments:
* 1) An array
* 2) A function
* 3) A seed
* Objectives:
* 1) Call <function> for every element in <collection> passing the arguments:
* previous result, element, index
* 2) Use the return value of <function> as the "previous result"
* for the next iteration
* 3) On the very first iteration, use <seed> as the "previous result"
* 4) If no <seed> was given, use the first element/value of <collection> as <seed> and continue to the next element
* 5) After the last iteration, return the return value of the final <function> call
* Edge Cases:
* 1) What if <seed> is not given?
* Examples:
* _.reduce([1,2,3], function(previousSum, currentValue, currentIndex){ return previousSum + currentValue }, 0) -> 6
*/
_.reduce = function (array, func, seed) {
// create a varible store starting index to = 0
let start = 0
let result = seed
if (seed === undefined) {
// set the starting index to 1
start = 1
result = array[0]
}
for (let i = start; i < array.length; i++) {
result = func(result, array[i], i)
}
return result
}
/** _.extend
* Arguments:
* 1) An Object
* 2) An Object
* ...Possibly more objects
* Objectives:
* 1) Copy properties from <object 2> to <object 1>
* 2) If more objects are passed in, copy their properties to <object 1> as well, in the order they are passed in.
* 3) Return the update <object 1>
* Examples:
* var data = {a:"one"};
* _.extend(data, {b:"two"}); -> data now equals {a:"one",b:"two"}
* _.extend(data, {a:"two"}); -> data now equals {a:"two"}
*/
_.extend = function (obj1, ...obj2) {
// loop through obj2
//console.log(...obj2)
// for(let key in obj2){
// console.log(obj2)
// obj1[key] = obj2[key]
// }
// assign the properties of obj2 to obj1
obj1 = Object.assign(obj1, ...obj2)
return obj1
}
//////////////////////////////////////////////////////////////////////
// DON'T REMOVE THIS CODE ////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
if ((typeof process !== 'undefined') &&
(typeof process.versions.node !== 'undefined')) {
// here, export any references you need for tests //
module.exports = _;
}