Q:

Write a JavaScript program to test a value, x, against a predicate function. If true, return fn(x). Else, return x

0

Write a JavaScript program to test a value, x, against a predicate function. If true, return fn(x). Else, return x.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

const when = (pred, whenTrue) => x => (pred(x) ? whenTrue(x) : x);
const doubleEvenNumbers = when(x => x % 2 === 0, x => x * 2);
console.log(doubleEvenNumbers(2));
console.log(doubleEvenNumbers(1));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now