Created
August 9, 2023 16:49
-
-
Save suhailgupta03/b6fa54be8dd583ed217b32ec466ada65 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Input 5 | |
// output 11 | |
// (5 + 5) + 1 | |
function multiply(n){ | |
return 2 * n | |
} | |
function add(n) { | |
return n + 1 | |
} | |
function compose(x, y) { | |
// x = add | |
// y = multiply | |
return function(n) { | |
// y(n) = 10 | |
return x(y(n)) | |
} | |
} | |
let multiplyAndAdd = compose(add, multiply) | |
var result = multiplyAndAdd(5) | |
console.log(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// Input 5
// output 11
// (5 + 5) + 1
// end-goal: first we want to double the number and then add 1
function multiply(n){
return 2 * n
}
function add(n) {
return n + 1
}
function compose(a, m) {
// a = add
// m = multiply
return function(number) {
const multResult = m(number)
const addResult = a(multResult);
return addResult
// return a(m(n))
// m(n) -> multResult
}
}
let multiplyAndAdd = compose(add, multiply)
var result = multiplyAndAdd(5)
console.log(result)