挑战1 addTwo
问题:
构建一个addTwo函数,作用为接受一个参数并将参数加2。
题解:
// 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));
挑战2 addS
问题:
构建一个addS函数,作用为接受一个参数并将参数与“S"拼接。
题解:
// Challenge 2
function addS(word) {
return word + "S";
}
// uncomment these to check your work
console.log(addS('pizza'));
console.log(addS('bagel'));
挑战3 map
问题:
构建一个map函数,其接受两个参数:
- 数值型数组
- 回调函数——一个应用于上述数值型数组中的每一个元素的函数(于map函数内)
map函数的返回值为包含上述数值型数组元素逐个运行回调函数后生成的元素值的新数组。
map([1,2,3,4,5], multiplyByTwo); //-> [2,4,6,8,10]
multiplyByTwo(1); //-> 2
multiplyByTwo(2); //-> 4
题解:
// Challenge 3
function map(array, callback) {
var newArray = [];
for (let i = 0; i < array.length; i++) {
newArray.push(callback(array[i]));
}
return newArray;
}
console.log(map([1, 2, 3], addTwo));
挑战4forEach
问题:
函数forEach接受一个数组和一个回调函数,运行回调函数于输入数组的每一个元素。forEach函数无返回值。
let alphabet = '';
const letters = ['a', 'b', 'c', 'd'];
forEach(letters, function(char) {
alphabet += char;
});
console.log(alphabet); //prints 'abcd'
题解:
// Challenge 4
function forEach(array, callback) {
for (let i =0; i < array.length; i++) {
callback(array[i]);
}
}
// see for yourself if your forEach works!
let alphabet = '';
const letters = ['a', 'b', 'c', 'd'];
forEach(letters, function(char) {
alphabet += char;
});
console.log(alphabet); //prints 'abcd'
挑战5 mapWith
问题:
在这个挑战中,你需要将map函数重构为mapWith。这一次你要在mapWith中使用forEach函数而不是使用for循环。
题解:
// Challenge 5
function mapWith(array, callback) {
var newArray = [];
forEach(array, function(item){
newArray.push(callback(item))});
return newArray;
}
console.log(mapWith([1, 2, 3], addTwo));
挑战6 reduce
问题:
函数reduce接受一个数组并将数组内的所有值合并为一个值。比如,它可以将数组求和,求积,以及其它你想加进函数中的操作。
const nums = [4, 1, 3];
const add = function(a, b) {
return a + b; }
reduce(nums, add, 0); //-> 8
以下是它的运行原理。函数有一个“累加器值”(第三个参数),作用为充当初始值并且累加每一次循环的输出值。数组参数会被遍历,传递“累加器值“和新的数组元素值作为参数到回调函数中。回调函数的返回值会成为新的”累加器值“。下一个循环会使用这个新”累加器值“。在上面的例子中,”累加器值“刚开始为0,调用add(0, 4),”累加器值“变为4,然后add(4, 1)将其变为5,最后add(5, 3)得到8并最终返回。
题解:
// Challenge 6
function reduce(array, callback, initialValue) {
// // Solution 1:
// var callbackValue = 0;
// var finalResult = 0;
// for(let i = 0; i<array.length; i++) {
// if (i == 0){
// callbackValue = array[i];
// finalResult = callback(callbackValue, initialValue);
// } else {
// finalResult = callback(finalResult, array[i]);
// }
// }
// return finalResult;
// Solution 2:
let reduceValue = initialValue;
for(let i = 0; i < array.length; i++){
reduceValue = callback(array[i], reduceValue);
}
return reduceValue;
}
const nums = [4, 1, 3];
const add = function(a, b) {
return a + b; }
console.log(reduce(nums, add, 0)); //-> 8
挑战7 intersection
问题:
构建intersection函数,作用为比较输入进来的多组数组并返回一个包含数组间共同元素的新数组。奖励:使用reduce!
题解:
// Challenge 7
function intersection(arrays) {
// Solution 1 (without reduce):
// var obj = {};
// for(let i=0; i<arrays.length; i++){
// if(i==0){
// for(var j=0;j<arrays[i].length; j++){
// obj[arrays[i][j]]=arrays[i][j];
// }
// } else {
// for(var j=0;j<arrays[i].length; j++){
// // console.log(obj[arrays[i][j]])
// if(obj[arrays[i][j]] != undefined){
// obj[arrays[i][j]]= obj[arrays[i][j]] + 1;
// }
/