Skip to content

Commit

Permalink
feat(Recursion): reverse() 함수 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
2wndrhs committed Jan 8, 2024
1 parent b5baf50 commit 26ee923
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Recursion/reverse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Problem
// 문자열의 역순인 문자열을 반환하는 `reverse` 함수를 작성하라.

// Input
// 알파벳으로 이루어진 문자열 `string`

// Output
// 주어진 문자열의 역순인 문자열을 반환

// 문자열의 길이가 2보다 작을 경우 해당 문자열을 반환 (base case)
// 문자열의 마지막 문자와 해당 문자를 제외한 문자열로 함수를 재귀호출한 결과를 더한 값을 반환

const reverse = (string: string): string => {
if (string.length < 2) {
return string;
}

return string[string.length - 1] + reverse(string.slice(0, string.length - 1));
};

export default reverse;

0 comments on commit 26ee923

Please sign in to comment.