24 files changed

+285
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function addTwo(num) {
2+
return num + 2;
3+
}
4+
5+
// To check if you've completed it, uncomment these console.logs!
6+
// console.log(addTwo(3));
7+
// console.log(addTwo(10));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function addS(word) {
2+
return word + 's';
3+
}
4+
5+
// uncomment these to check your work
6+
// console.log(addS('pizza'));
7+
// console.log(addS('bagel'));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function map(array, callback) {
2+
const result = [];
3+
for (const el of array) {
4+
result.push(callback(el));
5+
}
6+
return result;
7+
}
8+
9+
// console.log(map([1, 2, 3], addTwo));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function forEach(array, callback) {
2+
for (let i = 0; i < array.length; i++) {
3+
callback(array[i], i, array);
4+
}
5+
}
6+
7+
// forEach([1, 2, 3], (el) => console.log(el))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function mapWith(array, callback) {
2+
const result = [];
3+
array.forEach(el => result.push(callback(el)));
4+
return result;
5+
}
6+
7+
// console.log(mapWith([1, 2, 3], addTwo));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function reduce(array, callback, initialValue) {
2+
for (let i = 0; i < array.length; i++) {
3+
initialValue = callback(initialValue, array[i]);
4+
}
5+
return initialValue;
6+
}
7+
8+
const add = (acc, el) => acc + el;
9+
console.log(reduce([1, 2, 3], add, 0));
Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function union(...arrays) {
2+
return arrays.reduce((acc, arr) => {
3+
arr.forEach(el => {
4+
if (!acc.includes(el)) {
5+
acc.push(el);
6+
}
7+
});
8+
return acc;
9+
})
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function objOfMatches(array1, array2, callback) {
2+
return array1.reduce((res, el, i) => {
3+
if (callback(el) === array2[i]) {
4+
res[el] = array2[i];
5+
}
6+
return res;
7+
}, {})
8+
}
9+
10+
console.log(objOfMatches(['hi', 'howdy', 'bye', 'later', 'hello'], ['HI', 'Howdy', 'BYE', 'LATER', 'hello'], function(str) { return str.toUpperCase(); })); // should log: { hi: 'HI', bye: 'BYE', later: 'LATER' }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function multiMap(arrVals, arrCallbacks) {
2+
return arrVals.reduce((res, key) => {
3+
res[key] = arrCallbacks.map(callback => callback(key))
4+
return res;
5+
}, {})
6+
}
7+
8+
console.log(multiMap(['catfood', 'glue', 'beer'], [function(str) { return str.toUpperCase(); }, function(str) { return str[0].toUpperCase() + str.slice(1).toLowerCase(); }, function(str) { return str + str; }]));
9+
// should log: { catfood: ['CATFOOD', 'Catfood', 'catfoodcatfood'], glue: ['GLUE', 'Glue', 'glueglue'], beer: ['BEER', 'Beer', 'beerbeer'] }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function objectFilter(obj, callback) {
2+
return Object.keys(obj).reduce((res, key) => {
3+
if (obj[key] === callback(key)) {
4+
res[key] = obj[key];
5+
}
6+
return res;
7+
}, {})
8+
}
9+
10+
const cities = {
11+
London: 'LONDON',
12+
LA: 'Los Angeles',
13+
Paris: 'PARIS',
14+
};
15+
console.log(objectFilter(cities, city => city.toUpperCase())) // Should log { London: 'LONDON', Paris: 'PARIS'}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function majority(array, callback) {
2+
count = {
3+
true: 0,
4+
false: 0
5+
}
6+
7+
array.forEach(el => count[callback(el)]++)
8+
9+
if (count[true] > count[false]) {
10+
return true;
11+
} else {
12+
return false;
13+
}
14+
}
15+
16+
const isOdd = function(num) { return num % 2 === 1; };
17+
console.log(majority([1, 2, 3, 4, 5], isOdd)); // should log: true
18+
console.log(majority([2, 3, 4, 5], isOdd)); // should log: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function prioritize(array, callback) {
2+
const res = {
3+
true: [],
4+
false: []
5+
}
6+
7+
array.forEach(el => res[callback(el)].push(el));
8+
return res[true].concat(res[false]);
9+
}
10+
11+
// /*** Uncomment these to check your work! ***/
12+
const startsWithS = function(str) { return str[0] === 's' || str[0] === 'S'; };
13+
console.log(prioritize(['curb', 'rickandmorty', 'seinfeld', 'sunny', 'friends'], startsWithS));
14+
// should log: ['seinfeld', 'sunny', 'curb', 'rickandmorty', 'friends']
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function countBy(array, callback) {
2+
return array.reduce((res, el) => {
3+
const key = callback(el);
4+
res[key] = res[key] !== undefined ? ++res[key] : 1;
5+
return res;
6+
}, {})
7+
}
8+
9+
// /*** Uncomment these to check your work! ***/
10+
console.log(countBy([1, 2, 3, 4, 5], function(num) {
11+
if (num % 2 === 0) return 'even';
12+
else return 'odd';
13+
})); // should log: { odd: 3, even: 2 }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function groupBy(array, callback) {
2+
return array.reduce((res, el) => {
3+
const key = callback(el);
4+
if (res[key] !== undefined) {
5+
res[key].push(el);
6+
} else {
7+
res[key] = [el];
8+
};
9+
return res;
10+
}, {})
11+
}
12+
13+
const decimals = [1.3, 2.1, 2.4];
14+
const floored = function(num) { return Math.floor(num); };
15+
console.log(groupBy(decimals, floored)); // should log: { 1: [1.3], 2: [2.1, 2.4] }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function goodKeys(obj, callback) {
2+
return Object.keys(obj).filter(key => callback(obj[key]));
3+
}
4+
5+
const sunny = { mac: 'priest', dennis: 'calculating', charlie: 'birdlaw', dee: 'bird', frank: 'warthog' };
6+
const startsWithBird = function(str) { return str.slice(0, 4).toLowerCase() === 'bird'; };
7+
console.log(goodKeys(sunny, startsWithBird)); // should log: ['charlie', 'dee']
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function commutative(func1, func2, value) {
2+
const firstRes = func1(func2(value));
3+
const secondRes = func2(func1(value));
4+
return firstRes === secondRes;
5+
}
6+
7+
// /*** Uncomment these to check your work! ***/
8+
const multBy3 = n => n * 3;
9+
const divBy4 = n => n / 4;
10+
const subtract5 = n => n - 5;
11+
console.log(commutative(multBy3, divBy4, 11)); // should log: true
12+
console.log(commutative(multBy3, subtract5, 10)); // should log: false
13+
console.log(commutative(divBy4, subtract5, 48)); // should log: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function objFilter(obj, callback) {
2+
return Object.keys(obj).reduce((res, key) => {
3+
if (callback(key) === obj[key]) {
4+
res[key] = obj[key];
5+
}
6+
return res;
7+
}, {});
8+
}
9+
10+
const startingObj = {};
11+
startingObj[6] = 3;
12+
startingObj[2] = 1;
13+
startingObj[12] = 4;
14+
const half = n => n / 2;
15+
console.log(objFilter(startingObj, half)); // should log: { 2: 1, 6: 3 }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function rating(arrOfFuncs, value) {
2+
const trueArr = arrOfFuncs.filter(func => func(value));
3+
const percentage = (trueArr.length / arrOfFuncs.length) * 100;
4+
return percentage;
5+
}
6+
7+
const isEven = n => n % 2 === 0;
8+
const greaterThanFour = n => n > 4;
9+
const isSquare = n => Math.sqrt(n) % 1 === 0;
10+
const hasSix = n => n.toString().includes('6');
11+
const checks = [isEven, greaterThanFour, isSquare, hasSix];
12+
console.log(rating(checks, 64)); // should log: 100
13+
console.log(rating(checks, 66)); // should log: 75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function pipe(arrOfFuncs, value) {
2+
return arrOfFuncs.reduce((acc, func) => func(acc), value);
3+
}
4+
5+
const capitalize = str => str.toUpperCase();
6+
const addLowerCase = str => str + str.toLowerCase();
7+
const repeat = str => str + str;
8+
const capAddlowRepeat = [capitalize, addLowerCase, repeat];
9+
console.log(pipe(capAddlowRepeat, 'cat')); // should log: 'CATcatCATcat'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function highestFunc(objOfFuncs, subject) {
2+
const keys = Object.keys(objOfFuncs);
3+
return keys.reduce((prevKey, key) => {
4+
const prevFuncRes = objOfFuncs[prevKey](subject);
5+
const curFuncRes = objOfFuncs[key](subject);
6+
if (curFuncRes > prevFuncRes) {
7+
return key;
8+
} else {
9+
return prevKey;
10+
}
11+
}, keys[0])
12+
}
13+
14+
const groupOfFuncs = {};
15+
groupOfFuncs.double = n => n * 2;
16+
groupOfFuncs.addTen = n => n + 10;
17+
groupOfFuncs.inverse = n => n * -1;
18+
console.log(highestFunc(groupOfFuncs, 5)); // should log: 'addTen'
19+
console.log(highestFunc(groupOfFuncs, 11)); // should log: 'double'
20+
console.log(highestFunc(groupOfFuncs, -20)); // should log: 'inverse'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function combineOperations(startVal, arrOfFuncs) {
2+
return arrOfFuncs.reduce((acc, func) => func(acc), startVal);
3+
}
4+
5+
function add100(num) {
6+
return num + 100;
7+
}
8+
9+
function divByFive(num) {
10+
return num / 5;
11+
}
12+
13+
function multiplyByThree(num) {
14+
return num * 3;
15+
}
16+
17+
function multiplyFive(num) {
18+
return num * 5;
19+
}
20+
21+
function addTen(num) {
22+
return num + 10;
23+
}
24+
25+
console.log(combineOperations(0, [add100, divByFive, multiplyByThree])); // Should output 60 -->
26+
console.log(combineOperations(0, [divByFive, multiplyFive, addTen])); // Should output 10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function myFunc(array, callback) {
2+
for (let i = 0; i < array.length; i++) {
3+
if (callback(array[i])) return i;
4+
}
5+
return -1;
6+
}
7+
8+
const numbers = [2, 3, 6, 64, 10, 8, 12];
9+
const evens = [2, 4, 6, 8, 10, 12, 64];
10+
11+
function isOdd(num) {
12+
return (num % 2 !== 0);
13+
}
14+
15+
console.log(myFunc(numbers, isOdd)); // Output should be 1
16+
console.log(myFunc(evens, isOdd)); // Output should be -1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function myForEach(array, callback) {
2+
for (let i = 0; i < array.length; i++) {
3+
callback(array[i], i, array);
4+
}
5+
}
6+
7+
let sum = 0;
8+
9+
function addToSum(num) {
10+
sum += num;
11+
}
12+
13+
// /*** Uncomment these to check your work! ***/
14+
const nums = [1, 2, 3];
15+
myForEach(nums, addToSum);
16+
console.log(sum); // Should output 6

0 commit comments

Comments
 (0)