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; 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;