abidh186 / callback_assignment

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Callback Exercises

  1. Write a function forEachElem that takes in an array and a callback. Call the callback on each element in the array.
  2. Create a function logEachElem that takes an array as an argument, and uses the forEachElem method to log each element of the array.
  3. Create a function logArrTypes that uses the forEachElem 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
  1. 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.
  2. Create a function 'allCapsthat takes in an array as an argument, and uses themyMap` function to return all the elements capitalized.
  3. 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!"

About


Languages

Language:JavaScript 100.0%