From 714e64d2b8616de233536730221aacbcb31b625a Mon Sep 17 00:00:00 2001 From: 7assentlili Date: Sun, 3 Nov 2019 22:49:17 +0100 Subject: [PATCH] add includesAny --- snippets/includesAny.md | 17 +++++++++++++++++ test/includesAny.test.js | 14 ++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 snippets/includesAny.md create mode 100644 test/includesAny.test.js diff --git a/snippets/includesAny.md b/snippets/includesAny.md new file mode 100644 index 000000000..e8a701fc5 --- /dev/null +++ b/snippets/includesAny.md @@ -0,0 +1,17 @@ +--- +title: includesAny +tags: array,beginner +--- + +Returns `true` if at least one element of values is included in arr , `false` otherwise. + +Use `Array.prototype.some()` and `Array.prototype.includes()` to check if at least one element of `values` is included in `arr`. + +```js +const includesAny = (arr, values) => values.some(v => arr.includes(v)); +``` + +```js +includesAny([1, 2, 3, 4], [2, 9]); // true +includesAny([1, 2, 3, 4], [8, 9]); // false +``` diff --git a/test/includesAny.test.js b/test/includesAny.test.js new file mode 100644 index 000000000..a91bdcd63 --- /dev/null +++ b/test/includesAny.test.js @@ -0,0 +1,14 @@ +const {includesAny} = require('./_30s.js'); + +test('any is a Function', () => { + expect(includesAny).toBeInstanceOf(Function); +}); +test('Returns true when values contains one element of arr', () => { + expect(includesAny([0, 1, 2, 3], [1, 10, 20])).toBe(true); +}); +test('Returns false when values contains none of arr elements', () => { + expect(includesAny([0, 1, 2, 3], [10, 20, 30])).toBe(false); +}); +test('Returns false when values is an empty array', () => { + expect(includesAny([0, 1, 2, 3], [])).toBe(false); +});