Merge pull request #1040 from 7assenTlili/pr/includesAny
add includesAny
This commit is contained in:
17
snippets/includesAny.md
Normal file
17
snippets/includesAny.md
Normal 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
14
test/includesAny.test.js
Normal 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);
|
||||
});
|
||||
Reference in New Issue
Block a user