add includesAll
This commit is contained in:
17
snippets/includesAll.md
Normal file
17
snippets/includesAll.md
Normal file
@ -0,0 +1,17 @@
|
||||
---
|
||||
title: includesAll
|
||||
tags: array,beginner
|
||||
---
|
||||
|
||||
Returns `true` if all the elements of values are included in arr , `false` otherwise.
|
||||
|
||||
Use `Array.prototype.every()` and `Array.prototype.includes()` to check if all elements of `values` are included in `arr`.
|
||||
|
||||
```js
|
||||
const includesAll = (arr, values) => values.every(v => arr.includes(v));
|
||||
```
|
||||
|
||||
```js
|
||||
includesAll([1, 2, 3, 4], [1, 4]); // true
|
||||
includesAll([1, 2, 3, 4], [1, 5]); // false
|
||||
```
|
||||
14
test/includesAll.test.js
Normal file
14
test/includesAll.test.js
Normal file
@ -0,0 +1,14 @@
|
||||
const {includesAll} = require('./_30s.js');
|
||||
|
||||
test('any is a Function', () => {
|
||||
expect(includesAll).toBeInstanceOf(Function);
|
||||
});
|
||||
test('Returns true when all values are included in arr', () => {
|
||||
expect(includesAll([0, 1, 2, 3], [0, 1])).toBe(true);
|
||||
});
|
||||
test('Returns false when one element fo values is not included in arr', () => {
|
||||
expect(includesAll([0, 1, 2, 3], [1, 2, 4])).toBe(false);
|
||||
});
|
||||
test('Returns false when values is larger than arr', () => {
|
||||
expect(includesAll([0, 1, 2], [0, 1, 2, 3])).toBe(false);
|
||||
});
|
||||
Reference in New Issue
Block a user