Last active
February 2, 2020 17:56
-
-
Save skhashaev/dbba7c141ba60a25feb10dddafb566b5 to your computer and use it in GitHub Desktop.
JS Twist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Challenge 1 | |
function addTwo(num) { | |
return num + 2; | |
} | |
// To check if you've completed it, uncomment these console.logs! | |
// console.log(addTwo(3)); | |
// console.log(addTwo(10)); | |
// Challenge 2 | |
function addS(word) { | |
return word + "s"; | |
} | |
// uncomment these to check your work | |
// console.log(addS('pizza')); | |
// console.log(addS('bagel')); | |
// Challenge 3 | |
function map(array, callback) { | |
let newArray = []; | |
newArray.forEach(callback) | |
for (let i = 0; i < array.length; i++) { | |
newArray.push(callback(array[i])); | |
} | |
return newArray; | |
} | |
// console.log(map([1, 2, 3], addTwo)); | |
// Challenge 4 | |
function forEach(array, callback) { | |
for (let i = 0; i < array.length; i++) { | |
callback(array[i]); | |
} | |
} | |
// check below--------------- | |
// let newArray = []; | |
// let array = [1, 2, 3] | |
// forEach(array, function(item) { | |
// newArray.push(item * 2); | |
// }); | |
// console.log(newArray); | |
// see for yourself if your forEach works! | |
// check above--------------- | |
//-------------------------------------------------- | |
// Extension | |
//-------------------------------------------------- | |
//Extension 1 | |
function mapWith(array, callback) { | |
let newArray = []; | |
forEach(array, function(item) { | |
newArray.push(callback(item)); | |
}); | |
return newArray; | |
} | |
// mapWith([1, 2, 3], addTwo); | |
//Extension 2 | |
function reduce(array, callback, initialValue) { | |
let result = initialValue; | |
for (let i = 0; i < array.length; i++) { | |
result = callback(result, array[i]); | |
} | |
return result; | |
} | |
// console.log(reduce([1, 2, 3, 4, 5], function(a, b) { return a + b; }, 0)); | |
//Extension 3 | |
function intersection(arrays) { | |
let allArrays = []; | |
for (let arr of arguments) { | |
allArrays.push(arr); | |
} | |
// console.log(allArrays); | |
return reduce(allArrays, function(resultArr, arr) { | |
// forEach(arr, function(num) { | |
let wrongs = []; | |
for (let j = 0; j < arr.length; j++) { | |
for (let i = 0; i < allArrays.length; i++) { | |
if (allArrays[i].indexOf(arr[j]) === - 1) { | |
wrongs.push(arr[j]); | |
} | |
} | |
if (wrongs.indexOf(arr[j]) === -1 && resultArr.indexOf(arr[j]) === -1) { | |
resultArr.push(arr[j]) | |
} | |
}; | |
return resultArr; | |
}, []); | |
} | |
// console.log(intersection([5, 10, 15, 20], [15, 88, 1, 5, 7], [1, 10, 15, 5, 20])); | |
// should log: [5, 15] | |
//Extension 4 | |
function union(arrays) { | |
return reduce(arguments, function (resultArr, arr) { | |
for (let i = 0; i < arr.length; i++) { | |
if (resultArr.indexOf(arr[i]) === -1) { | |
resultArr.push(arr[i]) | |
} | |
} | |
return resultArr; | |
}, []); | |
} | |
// console.log(union([5, 10, 15], [15, 88, 1, 5, 7], [100, 15, 10, 1, 5])); | |
// should log: [5, 10, 15, 88, 1, 7, 100] | |
//Extension 5 | |
function objOfMatches(array1, array2, callback) { | |
let obj = {}; | |
function checker(check1) { | |
return callback(check1) | |
} | |
for (let i = 0; i < array1.length; i++) { | |
if (checker(array1[i]) === array2[i]) { | |
obj[array1[i]] = array2[i]; | |
} | |
} | |
return obj; | |
} | |
// 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' } | |
//Extension 6 | |
function multiMap(arrVals, arrCallbacks) { | |
let obj = {}; | |
function callbacks(value) { | |
let array = []; | |
for (let i = 0; i < arrCallbacks.length; i++) { | |
array.push(arrCallbacks[i](value)); | |
} | |
return array; | |
} | |
for (let i = 0; i < arrVals.length; i++) { | |
obj[arrVals[i]] = callbacks(arrVals[i]); | |
} | |
return obj; | |
} | |
// 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; }])); | |
// should log: { catfood: ['CATFOOD', 'Catfood', 'catfoodcatfood'], glue: ['GLUE', 'Glue', 'glueglue'], beer: ['BEER', 'Beer', 'beerbeer'] } | |
//Extension 7 | |
function objectFilter(obj, callback) { | |
const newObject = Object.entries(obj); | |
function filter(item) { | |
return callback(item); | |
} | |
for (let i = 0; i < newObject.length; i++) { | |
if (newObject[i][1] !== filter(newObject[i][1])) { | |
newObject.splice(i, 1) | |
} | |
} | |
return newObject; | |
} | |
// const cities = { | |
// London: 'LONDON', | |
// LA: 'Los Angeles', | |
// Paris: 'PARIS', | |
// }; | |
// console.log(objectFilter(cities, city => city.toUpperCase())) | |
// Should log { London: 'LONDON', Paris: 'PARIS'} | |
//Extension 5
function objOfMatches(array1, array2, callback) {
let obj = {};
for (let i = 0; i < array1.length; i++) {
if (callback(array1[i]) === array2[i]) {
obj[array1[i]] = array2[i];
}
}
return obj;
}
убрал checker
, так как он просто так существует))
//Extension 6
function multiMap(arrVals, arrCallbacks) {
const obj = {};
forEach(arrVals, el => {
obj[el] = map(arrCallbacks, cb => cb(el))
});
return obj;
}
а тут я воспользуюсь тобою созданными инструментами:) (немного функционального программирования)
Так реально намного легче твой код читается, просто надо привыкнуть
//Extension 7
function objectFilter(obj, callback) {
const newObject = Object.entries(obj);
for (let i = 0; i < newObject.length; i++) {
if (newObject[i][1] !== callback(newObject[i][1])) {
newObject.splice(i, 1)
}
}
return newObject;
}
filter
- лишняя функция
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
//Extension 4
includes
- реально бомба, забывай проindexOf
;)