- Write a function
forEachElem
that takes in an array and a callback. Call the callback on each element in the array. - Create a function logEachElem that takes an array as an argument, and uses the
forEachElem
method to log each element of the array. - Create a function
logArrTypes
that uses theforEachElem
method to log each array element followed by its type. For example:
let mixedArr = [1, 'cat', true]
logArrTypes(mixedArr)
// will log:
// 1 is a number
// 'cat' is a string
// true is a boolean
- Write a function
myMap
that takes in an array and a callback. Call the callback on each element of the array and add the result to an output array. Return the output array. - Create a function 'allCaps
that takes in an array as an argument, and uses the
myMap` function to return all the elements capitalized. - You are given the following functions:
function conservativeSpender(balance) {
return balance > 100
}
function liberalSpender(balance) {
return balance > 10
}
function horribleSaver (balance) {
return balance > 0
}
Write a function shouldIBuyThis
that takes in a balance and a callback (one of the above functions). The fuction should return either
"Sure! I've got the money!" or "Nope! Gotta keep my savings up!"
console.log(shouldIBuyThis(20, horribleSaver))
// logs: "Sure! I've got the money!"
console.log(shouldIBuyThis(20, liberalSpender))
// logs: "Sure! I've got the money!"
console.log(shouldIBuyThis(20, conservativeSpender))
// logs: "Nope! Gotta keep my savings up!"
console.log(shouldIBuyThis(101, conservativeSpender))
// logs: "Sure! I've got the money!"