From 59183e03b3483ce72b29b1479829bba9ab4bbe83 Mon Sep 17 00:00:00 2001 From: Bruce Feldman Date: Sun, 12 Aug 2018 21:01:48 -0400 Subject: [PATCH] Simplified nth Element --- snippets/nthElement.md | 4 ++-- test/nthElement/nthElement.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/nthElement.md b/snippets/nthElement.md index 6a9268d9a..a3c3f427b 100644 --- a/snippets/nthElement.md +++ b/snippets/nthElement.md @@ -3,11 +3,11 @@ Returns the nth element of an array. Use `Array.slice()` to get an array containing the nth element at the first place. -If the index is out of bounds, return `[]`. +If the index is out of bounds, return `undefined`. Omit the second argument, `n`, to get the first element of the array. ```js -const nthElement = (arr, n = 0) => (n > 0 ? arr.slice(n, n + 1) : arr.slice(n))[0]; +const nthElement = (arr, n = 0) => (n === -1 ? arr.slice(n) : arr.slice(n, n + 1))[0]; ``` ```js diff --git a/test/nthElement/nthElement.js b/test/nthElement/nthElement.js index 210321c42..c9c0ffe21 100644 --- a/test/nthElement/nthElement.js +++ b/test/nthElement/nthElement.js @@ -1,2 +1,2 @@ -const nthElement = (arr, n = 0) => (n > 0 ? arr.slice(n, n + 1) : arr.slice(n))[0]; +const nthElement = (arr, n = 0) => (n === -1 ? arr.slice(n) : arr.slice(n, n + 1))[0]; module.exports = nthElement;