Skip to content

Commit 70e5b44

Browse files
committed
added problems
1 parent a7aaa85 commit 70e5b44

24 files changed

+285
-0
lines changed

csbin/callbacks/01-addTwo.js

Lines changed: 7 additions & 0 deletions
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));

csbin/callbacks/02-addS.js

Lines changed: 7 additions & 0 deletions
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'));

csbin/callbacks/03-map.js

Lines changed: 9 additions & 0 deletions
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));

csbin/callbacks/04-forEach.js

Lines changed: 7 additions & 0 deletions
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))

csbin/callbacks/05-mapWith.js

Lines changed: 7 additions & 0 deletions
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));

csbin/callbacks/06-reduce.js

Lines changed: 9 additions & 0 deletions
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));

csbin/callbacks/07-intersection.js

Whitespace-only changes.

csbin/callbacks/08-union.js

Lines changed: 10 additions & 0 deletions
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+
}

csbin/callbacks/09-objOfMatches.js

Lines changed: 10 additions & 0 deletions
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' }

csbin/callbacks/10-multiMap.js

Lines changed: 9 additions & 0 deletions
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'] }

0 commit comments

Comments
 (0)