From 56a3cddb7b04d5bc26b61ad92410c7ba4e72f4f3 Mon Sep 17 00:00:00 2001 From: chaeminseok Date: Sun, 13 Aug 2023 23:37:52 +0900 Subject: [PATCH 1/2] Feat: dropRight.js --- dropRight.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 dropRight.js diff --git a/dropRight.js b/dropRight.js new file mode 100644 index 0000000..4af6c8b --- /dev/null +++ b/dropRight.js @@ -0,0 +1,23 @@ +function dropRight(arr, n = 1) { + if (!Array.isArray(arr)) { + throw new Error('배열을 입력하세요!'); + } + + if (arr.length === 0) { + return []; + } + if (n <= 0) { + return arr; + } + + const result = []; + + for (let i = 0; i < arr.length - n; i++) { + result.push(arr[i]); + } + + return result; +} +//console.log(dropRight([1, 2, 3], 2)); + +export default dropRight; From 2dd75d25332d7a140216a157274cb4fef75c925a Mon Sep 17 00:00:00 2001 From: chaeminseok Date: Mon, 21 Aug 2023 02:40:31 +0900 Subject: [PATCH 2/2] Feat: add nth --- nth.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 nth.js diff --git a/nth.js b/nth.js new file mode 100644 index 0000000..825b8cb --- /dev/null +++ b/nth.js @@ -0,0 +1,19 @@ +function nth(arr, n = 0) { + if (!Array.isArray(arr)) { + throw new Error('배열을 입력하세요!'); + } + // if (!Number.isInteger(n)) { + // return arr[0]; + // } + // if (n >= 0) { + // return arr[n]; + // } else if (n < 0) { + // return arr[arr.length + n]; + // } + return ( + Array.isArray(arr) && + (Number.isInteger(n) ? (n >= 0 ? arr[n] : arr[arr.length + n]) : arr[0]) + ); +} + +module.exports = nth;