Merge pull request #1040 from 7assenTlili/pr/includesAny

add includesAny
This commit is contained in:
Angelos Chalaris
2019-11-04 09:15:00 +02:00
committed by GitHub
2 changed files with 31 additions and 0 deletions

17
snippets/includesAny.md Normal file
View File

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

14
test/includesAny.test.js Normal file
View File

@ -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);
});