-
Notifications
You must be signed in to change notification settings - Fork 0
/
reduce.js
131 lines (89 loc) · 3.5 KB
/
reduce.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
/*
Write a function called extractValue which accepts an array of objects and a key and returns a
new array with the value of each object at the key.
Examples:
const arr = [{name: 'Elie'}, {name: 'Tim'}, {name: 'Matt'}, {name: 'Colt'}]
extractValue(arr,'name') // ['Elie', 'Tim', 'Matt', 'Colt']
*/
function extractValue(arr, key) {
// map immediately comes to mind :D
const outValues = arr.reduce(function (fxinArrValues, fxinNextValue) {
return fxinArrValues.concat(fxinNextValue[key]);
}, []);
return outValues;
}
/*
Write a function called vowelCount which accepts a string and returns an object with the keys
as the vowel and the values as the number of times the vowel appears in the string. This
function should be case insensitive so a lowercase letter and uppercase letter should count
Examples:
vowelCount('Elie') // {e:2,i:1};
vowelCount('Tim') // {i:1};
vowelCount('Matt') // {a:1})
vowelCount('hmmm') // {};
vowelCount('I Am awesome and so are you') // {i: 1, a: 4, e: 3, o: 3, u: 1};
*/
function vowelCount(str) {
const outVowelCountObj = str.split('').reduce(function (fxInVowelCounts, nextChar) {
if ("aeiou".indexOf(nextChar.toLowerCase()) > -1) {
if (fxInVowelCounts[nextChar]) {
fxInVowelCounts[nextChar] += 1;
} else {
fxInVowelCounts[nextChar] = 1;
}
}
return fxInVowelCounts;
}, {})
return outVowelCountObj;
}
/*
Write a function called addKeyAndValue which accepts an array of objects and returns the
array of objects passed to it with each object now including the key and value passed to the
function.
Examples:
const arr = [{name: 'Elie'}, {name: 'Tim'}, {name: 'Matt'}, {name: 'Colt'}];
addKeyAndValue(arr, 'title', 'Instructor') //
[
{title: 'Instructor', name: 'Elie'},
{title: 'Instructor', name: 'Tim'},
{title: 'Instructor', name: 'Matt'},
{title: 'Instructor', name: 'Colt'}
]
*/
function addKeyAndValue(arr, key, value) {
arr = arr.reduce(function (fxinObject, fxinNext) {
const newObj = fxinNext;
newObj[key] = value;
return fxinObject.concat(newObj);
}, []);
return arr;
}
/*
Write a function called partition which accepts an array and a callback and returns an array
with two arrays inside of it. The partition function should run the callback function on
each value in the array and if the result of the callback function at that specific value is
true, the value should be placed in the first subarray. If the result of the callback function
at that specific value is false, the value should be placed in the second subarray.
Examples:
function isEven(val){
return val % 2 === 0;
}
const arr = [1,2,3,4,5,6,7,8];
partition(arr, isEven) // [[2,4,6,8], [1,3,5,7]];
function isLongerThanThreeCharacters(val){
return val.length > 3;
}
const names = ['Elie', 'Colt', 'Tim', 'Matt'];
partition(names, isLongerThanThreeCharacters) // [['Elie', 'Colt', 'Matt'], ['Tim']]
*/
function partition(arr, callback) {
const outPartitionedArray = arr.reduce(function (fxinArrs, fxinVal) {
if (callback(fxinVal)) {
return [fxinArrs[0].concat(fxinVal), fxinArrs[1]];
} else {
return [fxinArrs[0], fxinArrs[1].concat(fxinVal)];
//return fxinArrs[1].concat(fxinVal);
}
}, [[], []]);
return outPartitionedArray;
}