-
Notifications
You must be signed in to change notification settings - Fork 0
/
_.js
103 lines (93 loc) · 2.32 KB
/
_.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
//https://youtu.be/av5JiKektzE
const _ = {
clamp(number, lower, upper) {
var lowerClampedValue = Math.max(number, lower);
var clampedValue = Math.min(lowerClampedValue, upper);
return clampedValue;
},
inRange(number, start, end){
if(end === undefined){
end = start
start = 0
}
if(start > end){
var temp = end
end = start
start = temp
}
var isInRange = start <= number && number < end
return isInRange
},
words(string){
var words = string.split(' ')
return words
},
pad(string, length){
if(length <= string.length){
return string
}
var startPaddingLength = Math.floor((length - string.length) / 2)
var endPaddingLength = length - string.length - startPaddingLength
var paddedString = ' '.repeat(startPaddingLength) + string + ' '.repeat(endPaddingLength)
return paddedString
},
//Codecademy's solution
/*has(object, key){
var hasValue = object[key];
if(hasValue != undefined){
return true
}return false
return hasValue
}*/
has(object, key){
var hasValue = object[key] != undefined
return hasValue
},
invert(object){
let invertedObject = {}
for(let key in object){
var originalValue = object[key]
invertedObject = {originalValue : key}
}
return invertedObject
},
findKey(object, predicate){
for(let key in object){
var value = object[key]
var predicateReturnValue = predicate(value)
if(predicateReturnValue){
return key
}
}
undefined
return undefined
},
drop(array, n){
if(n === undefined){
n = 1
}
var droppedArray = array.slice(n, array.length)
return droppedArray
},
dropWhile(array, predicate){
const cb = (element, index) => {
return !predicate(element, index, array)
}
var dropNumber = array.findIndex(cb)
var droppedArray = this.drop(array, dropNumber)
return droppedArray
},
chunk(array, size){
if(size === undefined){
size = 1
}
var arrayChunks = []
for(let i = 0; i < array.length; i+= size){
let arrayChunk = array.slice(i, i+size)
arrayChunks.push(arrayChunk)
}
return arrayChunks
}
}
// Do not write or modify code below this line.
module.exports = _;