Array-CB-Methods
Review of Concepts
1. Given multiple plausible reasons, identify why functions are called "First Class Objects" in JavaScript.
Here are some of the reasons:
A function is an instance of the Object type
A function can have properties and has a link back to its constructor method
You can store the function in a variable
You can pass the function as a parameter to another function
You can return the function from a function
2. Given a code snippet containing an anonymous callback, a named callback, and multiple console.logs, predict what will be printed
function foo(callback) {
console.log('grape');
callback();
}
function bar() {
console.log('banana');
}
const fruitBasket = function() {
console.log('apple');
bar();
foo(bar);
foo(function() {
console.log('orange');
});
console.log('pear');
};
fruitBasket();You should be able to predict what is going to be logged when we call fruitBasket.
3. Write a function that takes in a value and two callbacks. The function should return the result of the callback that is greater.
Note: we do not invoke negate or addOne (by using () to call them), we are passing the function itself.
4. Write a function, myMap, that takes in an array and a callback as arguments. The function should mimic the behavior of Array#map.
5. Write a function, myFilter, that takes in an array and a callback as arguments. The function should mimic the behavior of Array#filter.
6. Write a function, myEvery, that takes in an array and a callback as arguments. The function should mimic the behavior of Array#every.
Practice:
Last updated
Was this helpful?