diff --git a/src/array/index.js b/src/array/index.js index 06e4e2f..d19fc07 100644 --- a/src/array/index.js +++ b/src/array/index.js @@ -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'; \ No newline at end of file +export { default as longestStringLength } from './longest-string-length.js'; +export { default as oddItem } from './odd-item.js'; \ No newline at end of file diff --git a/src/array/odd-item.js b/src/array/odd-item.js new file mode 100644 index 0000000..1ac6339 --- /dev/null +++ b/src/array/odd-item.js @@ -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; diff --git a/test/array/index.spec.js b/test/array/index.spec.js index 270dd4b..431877d 100644 --- a/test/array/index.spec.js +++ b/test/array/index.spec.js @@ -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') +}); diff --git a/test/array/odd-item.spec.js b/test/array/odd-item.spec.js new file mode 100644 index 0000000..8a6c521 --- /dev/null +++ b/test/array/odd-item.spec.js @@ -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); +}); diff --git a/test/index.spec.js b/test/index.spec.js index ad364be..b06c8de 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -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') +});