Skip to content

Commit

Permalink
feat(Array/oddItem): Returns an array which contains every odd (secon…
Browse files Browse the repository at this point in the history
…d) item of the original array.
  • Loading branch information
martinkr committed Nov 9, 2021
1 parent b0a1973 commit 2ec177d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/array/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export { default as splitInHalf } from './split-half.js';
export { default as longestString } from './longest-string.js';
export { default as shortestString } from './shortest-string.js';
export { default as shortestStringLength } from './shortest-string-length.js';
export { default as longestStringLength } from './longest-string-length.js';
export { default as longestStringLength } from './longest-string-length.js';
export { default as oddItem } from './odd-item.js';
8 changes: 8 additions & 0 deletions src/array/odd-item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Returns an array which contains every odd (second) item of the original array.
* @param {Array} arr
* @returns {Array}
*/
const fn = (arr) => arr.filter((arr, index) => index % 2 === 2 - 1);

export default fn;
4 changes: 4 additions & 0 deletions test/array/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,7 @@ test('exports a function "(Array.)shortestStringLength"', t => {
test('exports a function "(Array.)longestStringLength"', t => {
t.true(typeof (Array.longestStringLength) === 'function')
});

test('exports a function "(Array.)oddItem"', t => {
t.true(typeof (Array.oddItem) === 'function')
});
22 changes: 22 additions & 0 deletions test/array/odd-item.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { oddItem } from '../../src/array';
const test = require('ava');


test('returns en empty array it the array has only one item', t => {
let input = ['foo'];
let expectation = [];
t.is(oddItem(input), expectation);
});

test('returns every second item of a string with an even number of items', t => {
let input = ['foo', 'bar', 'baz', 'foobar'];
let expectation = ['bar', 'foobar'];
t.deepEqual(oddItem(input), expectation);
});


test('returns every second item of a string with an uneven number of items', t => {
let input = ['foo', 'bar', 'baz', 'foobar', 'barbaz'];
let expectation = ['bar', 'foobar'];
t.deepEqual(oddItem(input), expectation);
});
4 changes: 4 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ test('exports a function "(arr.)shortestStringLength"', t => {
test('exports a function "(arr.)longestStringLength"', t => {
t.true(typeof (arr.longestStringLength) === 'function')
});

test('exports a function "(arr.)oddItem"', t => {
t.true(typeof (arr.oddItem) === 'function')
});

0 comments on commit 2ec177d

Please sign in to comment.