Files
30-seconds-of-code/snippets/complement.md
2020-09-15 21:52:00 +03:00

20 lines
412 B
Markdown

---
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
```