Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Created June 1, 2020 01:00
Show Gist options
  • Select an option

  • Save harrisonmalone/fc2952e30067ab5ef5a7dfc5c34e89db to your computer and use it in GitHub Desktop.

Select an option

Save harrisonmalone/fc2952e30067ab5ef5a7dfc5c34e89db to your computer and use it in GitHub Desktop.
// Problem 1
// Write a function called arrayMultiply that takes two numbers, and a callback function as arguments. Return the callback function with the two numbers (those arguments) multiplied together as its argument.
// Define an array outside of this function. Because JS scope works differently to Ruby you will be able to use that array within the following function without passing it through as an argument. But please note that it will not be functional programming as such - but in this case if you use map a new array will be created (rather than the original being mutated).
// Define a variable and in it store the result of arrayMultiply when called with 2, 2, and also an anonymous callback function that takes the result as an argument, and then multiplies each element in the array by that result. When you console.log this variable to screen it should produce [ 4, 8, 12 ].
const arr = [1, 2, 3]
function arrayMultiply(num1, num2, cb) {
const sum = num1 * num2
// => 2 * 2 = 4
return cb(sum)
}
const result = arrayMultiply(2, 2, function(sum) {
const multipliedArr = arr.map(function(value) {
return sum * value
// => 1 * 4 = 4
// => 2 * 4 = 8
// => 3 * 4 = 12
})
return multipliedArr
})
console.log(result)
// what is callback?
// passing a function as an argument to another function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment