From 30b8623259405f02cd23bc22866e290819e1dbda Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 13 May 2020 11:28:33 +0300 Subject: [PATCH] Add logical complement --- snippets/complement.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 snippets/complement.md diff --git a/snippets/complement.md b/snippets/complement.md new file mode 100644 index 000000000..8baec9a41 --- /dev/null +++ b/snippets/complement.md @@ -0,0 +1,19 @@ +--- +title: complement +tags: function,logic,beginner +--- + +Returns a function that is the logical complement of the given function, `fn`. + +Use the logical not (`!`) operator on the result of calling `fn` with any supplied `args`. + +```js +const complement = fn => (...args) => !fn(...args); +``` + +```js +const isEven = num => num % 2 === 0; +const isOdd = complement(isEven); +isOdd(2); // false +isOdd(3); // true +```