Challeng 1
Create a function which accepts a number as argument and adds 10 to the number.
function addTen(num) {
}
Challenge 2
Use the forEach function to add 10
to each element
let numbers = [10, 20, 30, 40]
Challenge 3
Use the map
function to add 10
to each element
let numbers = [10, 20, 30, 40]
Challenge 4
Implement a map function, which takes the array and a callback function which is applied to that array to transform the values. Use the for
loop within the map function to iterate over the array.
function map(array, callback) {
}
// console.log(map([1, 2, 3], addTen));
Challenge 5
Implement a forEach function, which iterates over each element and applies the function to each elment.
function forEach(array, callback) {
}
// see for yourself if your forEach works!
Challenge 6
Implement the map function, but instead of using a for loop, now use your custom forEach function to iterate over your array.
function mapWith(array, callback) {
}
Challenge 7
Use the filter function to filter out all the numbers smaller than 30 out of the array.
let numbers = [10, 20, 30, 40]
Challenge 8
Create a function which takes an array and a callback function as arguments. It then filters the array depending on your callback function.
function filter(array, callback) {
}
let numbers = [10, 20, 30, 40]
Challenge 9
Use the reduce function to add up all numbers within the array.
let numbers = [10, 20, 30, 40]
Challeng 1
function addTen(num) {
return num + 10
}
Challenge 2
const numbers = [10, 20, 30]
numbers.forEach(function(value, index, array) {
numbers[index] = numbers[index] + 10
})
console.log(numbers)
Challenge 3
let numbers = [10, 20, 30]
let result = numbers.map(function(value, index, array) {
return value + 10
})
console.log(result)
Challenge 4
function map(array, callback) {
let result = []
for(let i = 0; i < array.length; i++) {
// callback(item, index, array)
let newValue = callback(array[i], i, array)
result.push(newValue)
}
return result
}
map([10, 20, 30], function(value, index, array) {
return value + 10
});
// console.log(map([1, 2, 3], addTen));
Challenge 5
Implement a forEach function, which iterates over each element and applies the function to each elment.
let numbers = [10, 20, 30, 40]
function forEach(array, callback) {
for(let index = 0; index < array.length; index++) {
let value = array[index]
callback(value, index, array)
}
}
function addTen(value, index, array) {
array[index] = value + 10
}
forEach(numbers, addTen)
console.log(numbers1)
Challenge 6
In this challenge we want to use the forEach(array, callback)
function to replace the for
loop within our map(array, callback)
function to create a new map function mapWith(array, callback)
which no longer implements an iterative for
loop.
function map(array, callback) {
let result = []
// Goal is to replace this for loop with
// a forEach function
for(let i = 0; i < array.length; i++) {
let value = array[i]
let newValue = callback(value, i, array)
result.push(newValue)
}
return result
}
function forEach(array, callback) {
for (let i = 0; i < array.length; i++) {
let value = array[i];
callback(value, i, array);
}
}
function mapWith(array, callback) {
let result = []
forEach(array, function (value, index, array) {
let newValue = callback(value, index, array)
result.push(newValue)
})
return result
}
let array1 = [10, 20, 30, 40];
mapWith(array1, function(value) {
return value + 20
});
Gracias Dominik!