Update snippets head and last to type check

Update `head` and `last` to check on empty, undefined, null arrays.
This commit is contained in:
Christian Melgarejo Bresanovich
2019-12-11 03:17:18 -03:00
parent b26bc18ec3
commit ec28d5fa6f
4 changed files with 27 additions and 30 deletions

View File

@ -5,12 +5,16 @@ tags: array,beginner
Returns the head of a list. Returns the head of a list.
Use `arr[0]` to return the first element of the passed array. Check if `arr` is truthy and has a `length` property, use `arr[0]` if possible
to return the first element, otherwise return `undefined`
```js ```js
const head = arr => arr[0]; const head = arr => (arr && arr.length ? arr[0] : undefined);
``` ```
```js ```js
head([1, 2, 3]); // 1 head([1, 2, 3]); // 1
``` head([]); // undefined
head(null); // undefined
head(undefined); // undefined
```

View File

@ -5,12 +5,17 @@ tags: array,beginner
Returns the last element in an array. Returns the last element in an array.
Use `arr.length - 1` to compute the index of the last element of the given array and returning it. Check if `arr` is truthy and has a `length` property, use `arr.length - 1` to
compute the index of the last element of the given array and returning it,
otherwise return `undefined`
```js ```js
const last = arr => arr[arr.length - 1]; const last = arr => (arr && arr.length ? arr[arr.length - 1] : undefined);
``` ```
```js ```js
last([1, 2, 3]); // 3 last([1, 2, 3]); // 3
``` last([]); // undefined
last(null); // undefined
last(undefined); // undefined
```

View File

@ -15,20 +15,14 @@ test('head({ 0: false}) returns false', () => {
test('head(String) returns S', () => { test('head(String) returns S', () => {
expect(head('String')).toBe('S'); expect(head('String')).toBe('S');
}); });
test('head(null) throws an Error', () => { test('head(null) returns undefined', () => {
expect(() => { expect(head(null)).toBe(undefined);
head(null);
}).toThrow();
}); });
test('head(undefined) throws an Error', () => { test('head(undefined) returns undefined', () => {
expect(() => { expect(head(undefined)).toBe(undefined);
head(undefined);
}).toThrow();
}); });
test('head() throws an Error', () => { test('head() returns undefined', () => {
expect(() => { expect(head()).toBe(undefined);
head();
}).toThrow();
}); });
let start = new Date().getTime(); let start = new Date().getTime();
head([1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 1122, 32124, 23232]); head([1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 1122, 32124, 23232]);

View File

@ -15,20 +15,14 @@ test('last({ 0: false}) returns undefined', () => {
test('last(String) returns g', () => { test('last(String) returns g', () => {
expect(last('String')).toBe('g'); expect(last('String')).toBe('g');
}); });
test('last(null) throws an Error', () => { test('last(null) returns undefined', () => {
expect(() => { expect(last(null)).toBe(undefined);
last(null);
}).toThrow();
}); });
test('last(undefined) throws an Error', () => { test('last(undefined) returns undefined', () => {
expect(() => { expect(last(undefined)).toBe(undefined);
last(undefined);
}).toThrow();
}); });
test('last() throws an Error', () => { test('last() returns undefined', () => {
expect(() => { expect(last()).toBe(undefined);
last();
}).toThrow();
}); });
let start = new Date().getTime(); let start = new Date().getTime();
last([1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 1122, 32124, 23232]); last([1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 1122, 32124, 23232]);