Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add nth #19

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions dropRight.js
Original file line number Diff line number Diff line change
@@ -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;
19 changes: 19 additions & 0 deletions nth.js
Original file line number Diff line number Diff line change
@@ -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;