From e4677abc4bffc778f6a6135d1b99b0c9942b52d6 Mon Sep 17 00:00:00 2001 From: Eagerbeaver <86523545+YeongseoYoon@users.noreply.github.com> Date: Tue, 8 Aug 2023 16:00:32 +0900 Subject: [PATCH 01/15] solution: problem-1/solution-1 --- problem-1/problem-1.test.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/problem-1/problem-1.test.js b/problem-1/problem-1.test.js index c775a02..7609d19 100644 --- a/problem-1/problem-1.test.js +++ b/problem-1/problem-1.test.js @@ -1,18 +1,31 @@ +//1.가장 익숙한 방법으로 문제를 해결해 주세요. + const solution = (numbers) => { + let sum = 0; + if (numbers.length === 0) { + return 0; + } + for (let i = 0; i < numbers.length; i++) { + sum += parseInt(numbers[i], 10); + } + + return sum; }; +//2.이번에는 재귀 함수로 문제를 해결해 주세요. -test('빈 배열은 0을 반환한다', () => { +test("빈 배열은 0을 반환한다", () => { expect(solution([])).toBe(0); }); -test('배열의 합을 반환한다', () => { +test("배열의 합을 반환한다", () => { expect(solution([1, 2, 3, 4])).toBe(10); expect(solution([-1, 3, 8, 9, 10, 11])).toBe(40); }); -test('큰 배열이 입력으로 주어져도 RangeError를 던지지 않는다', () => { +test("큰 배열이 입력으로 주어져도 RangeError를 던지지 않는다", () => { const input = Array.from({ length: 10000 }, (_, i) => i + 1); - expect(() => solution(input)) - .not.toThrowError(new RangeError('Maximum call stack size exceeded')); + expect(() => solution(input)).not.toThrowError( + new RangeError("Maximum call stack size exceeded") + ); }); From 84c10a6df9e4c71e270d139ff6b45129ec9708c3 Mon Sep 17 00:00:00 2001 From: Eagerbeaver <86523545+YeongseoYoon@users.noreply.github.com> Date: Tue, 8 Aug 2023 16:25:26 +0900 Subject: [PATCH 02/15] solution: problem-1/solution-2 --- problem-1/problem-1.test.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/problem-1/problem-1.test.js b/problem-1/problem-1.test.js index 7609d19..9511bba 100644 --- a/problem-1/problem-1.test.js +++ b/problem-1/problem-1.test.js @@ -1,5 +1,6 @@ //1.가장 익숙한 방법으로 문제를 해결해 주세요. - +{ + /* const solution = (numbers) => { let sum = 0; if (numbers.length === 0) { @@ -10,9 +11,16 @@ const solution = (numbers) => { } return sum; -}; +};*/ +} //2.이번에는 재귀 함수로 문제를 해결해 주세요. - +const solution = (numbers, index = 0) => { + if (index >= numbers.length) { + return 0; + } else { + return numbers[index] + solution(numbers, index + 1); + } +}; test("빈 배열은 0을 반환한다", () => { expect(solution([])).toBe(0); }); From 56539235eb09550398921043405160dacb65c50d Mon Sep 17 00:00:00 2001 From: Eagerbeaver <86523545+YeongseoYoon@users.noreply.github.com> Date: Tue, 8 Aug 2023 16:37:09 +0900 Subject: [PATCH 03/15] solution: problem-1/solution-3,4 --- problem-1/problem-1.test.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/problem-1/problem-1.test.js b/problem-1/problem-1.test.js index 9511bba..fbe55d5 100644 --- a/problem-1/problem-1.test.js +++ b/problem-1/problem-1.test.js @@ -14,13 +14,31 @@ const solution = (numbers) => { };*/ } //2.이번에는 재귀 함수로 문제를 해결해 주세요. -const solution = (numbers, index = 0) => { +{ + /* + const solution = (numbers, index = 0) => { if (index >= numbers.length) { return 0; } else { return numbers[index] + solution(numbers, index + 1); } }; +*/ +} + +//3. 꼬리 재귀 함수로 바꿔보세요. +//4. 꼬리 재귀 최적화를 통해서 최적화해 보세요. +const solution = (numbers) => { + let index = 0; + let acc = 0; + + while (index < numbers.length) { + acc = acc + numbers[index]; + index++; + } + + return acc; +}; test("빈 배열은 0을 반환한다", () => { expect(solution([])).toBe(0); }); From 52a0f938df366b7dfe13bdd7bdbcb7f733f3cb2f Mon Sep 17 00:00:00 2001 From: Eagerbeaver <86523545+YeongseoYoon@users.noreply.github.com> Date: Tue, 8 Aug 2023 16:55:35 +0900 Subject: [PATCH 04/15] solution: problem-2/solution-1,2 --- problem-2/problem-2.test.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/problem-2/problem-2.test.js b/problem-2/problem-2.test.js index 458a6b8..dde4a23 100644 --- a/problem-2/problem-2.test.js +++ b/problem-2/problem-2.test.js @@ -1,16 +1,25 @@ +//1. 가장 익숙한 방법으로 문제를 해결해 주세요. +//2. 이번에는 재귀 함수로 문제를 해결해 주세요. const solution = (n) => { + if (n <= -1) { + return 0; + } else if (n === 0 || n === 1) { + return n; + } else { + return solution(n - 1) + solution(n - 2); + } }; -test('음수가 주어지면 0을 반환한다', () => { +test("음수가 주어지면 0을 반환한다", () => { expect(solution(-1)).toBe(0); }); -test('0부터 1까지는 정해진 수를 반환한다', () => { +test("0부터 1까지는 정해진 수를 반환한다", () => { expect(solution(0)).toBe(0); expect(solution(1)).toBe(1); }); -test('2이상 주어지면 앞 두 항의 합을 반환한다', () => { +test("2이상 주어지면 앞 두 항의 합을 반환한다", () => { expect(solution(2)).toBe(1); expect(solution(3)).toBe(2); expect(solution(4)).toBe(3); @@ -18,9 +27,10 @@ test('2이상 주어지면 앞 두 항의 합을 반환한다', () => { expect(solution(6)).toBe(8); }); -test('큰 입력이 주어져도 RangeError를 던지지 않는다', () => { +test("큰 입력이 주어져도 RangeError를 던지지 않는다", () => { const input = 100000; - expect(() => solution(input)) - .not.toThrowError(new RangeError('Maximum call stack size exceeded')); + expect(() => solution(input)).not.toThrowError( + new RangeError("Maximum call stack size exceeded") + ); }); From deda086a94fadd758477c55d473094bbb13d1398 Mon Sep 17 00:00:00 2001 From: Eagerbeaver <86523545+YeongseoYoon@users.noreply.github.com> Date: Tue, 8 Aug 2023 17:25:35 +0900 Subject: [PATCH 05/15] solution: problem-3/solution-1 --- problem-3/problem-3.test.js | 38 ++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/problem-3/problem-3.test.js b/problem-3/problem-3.test.js index 7319eb8..c6fb187 100644 --- a/problem-3/problem-3.test.js +++ b/problem-3/problem-3.test.js @@ -1,21 +1,33 @@ +//1. 가장 익숙한 방법으로 문제를 해결해 주세요. const solution = (n) => { + let binaryString = ""; + if (n <= 1) { + return "" + n; + } + while (n >= 1) { + binaryString += (n % 2).toString(); + n = parseInt(n / 2); + console.log(n); + } + return binaryString.split("").reverse().join(""); }; -test('이진수 문자열을 반환한다', () => { - expect(solution(0)).toBe('0'); - expect(solution(1)).toBe('1'); - expect(solution(2)).toBe('10'); - expect(solution(3)).toBe('11'); - expect(solution(4)).toBe('100'); - expect(solution(5)).toBe('101'); - expect(solution(6)).toBe('110'); - expect(solution(7)).toBe('111'); - expect(solution(8)).toBe('1000'); +test("이진수 문자열을 반환한다", () => { + expect(solution(0)).toBe("0"); + expect(solution(1)).toBe("1"); + expect(solution(2)).toBe("10"); + expect(solution(3)).toBe("11"); + expect(solution(4)).toBe("100"); + expect(solution(5)).toBe("101"); + expect(solution(6)).toBe("110"); + expect(solution(7)).toBe("111"); + expect(solution(8)).toBe("1000"); }); -test('큰 입력이 주어져도 RangeError를 던지지 않는다', () => { +test("큰 입력이 주어져도 RangeError를 던지지 않는다", () => { const input = Number.MAX_VALUE; - expect(() => solution(input)) - .not.toThrowError(new RangeError('Maximum call stack size exceeded')); + expect(() => solution(input)).not.toThrowError( + new RangeError("Maximum call stack size exceeded") + ); }); From 9f1e5eea917159a3b3613c182e25f56b079c82d3 Mon Sep 17 00:00:00 2001 From: Eagerbeaver <86523545+YeongseoYoon@users.noreply.github.com> Date: Tue, 8 Aug 2023 17:39:35 +0900 Subject: [PATCH 06/15] solution: problem-3/solution-2 --- problem-3/problem-3.test.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/problem-3/problem-3.test.js b/problem-3/problem-3.test.js index c6fb187..f71e05a 100644 --- a/problem-3/problem-3.test.js +++ b/problem-3/problem-3.test.js @@ -1,4 +1,6 @@ //1. 가장 익숙한 방법으로 문제를 해결해 주세요. +{ + /* const solution = (n) => { let binaryString = ""; if (n <= 1) { @@ -7,11 +9,20 @@ const solution = (n) => { while (n >= 1) { binaryString += (n % 2).toString(); n = parseInt(n / 2); - console.log(n); } return binaryString.split("").reverse().join(""); -}; +};*/ +} +//2. 이번에는 재귀 함수로 문제를 해결해 주세요. +const solution = (n, binaryString = "") => { + if (n <= 1) { + return n + binaryString; + } + const remainder = n % 2; + binaryString = remainder + binaryString; + return solution(parseInt(n / 2, 10), binaryString); +}; test("이진수 문자열을 반환한다", () => { expect(solution(0)).toBe("0"); expect(solution(1)).toBe("1"); From 1ef0165c9c882922417c3570f37db198dd426c34 Mon Sep 17 00:00:00 2001 From: Eagerbeaver <86523545+YeongseoYoon@users.noreply.github.com> Date: Tue, 8 Aug 2023 17:49:34 +0900 Subject: [PATCH 07/15] solution: problem-3/solution-3,4 --- problem-3/problem-3.test.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problem-3/problem-3.test.js b/problem-3/problem-3.test.js index f71e05a..cf3226f 100644 --- a/problem-3/problem-3.test.js +++ b/problem-3/problem-3.test.js @@ -14,6 +14,8 @@ const solution = (n) => { };*/ } //2. 이번에는 재귀 함수로 문제를 해결해 주세요. +{ + /* const solution = (n, binaryString = "") => { if (n <= 1) { return n + binaryString; @@ -23,6 +25,24 @@ const solution = (n, binaryString = "") => { binaryString = remainder + binaryString; return solution(parseInt(n / 2, 10), binaryString); }; +*/ +} + +//3. 꼬리 재귀 함수로 바꿔보세요. +//4. 꼬리 재귀 최적화를 통해서 최적화해 보세요. +const solution = (n) => { + let binaryString = ""; + if (n <= 1) { + return n + binaryString; + } + while (n >= 1) { + const remainder = n % 2; + binaryString = remainder + binaryString; + n = parseInt(n / 2); + } + return binaryString; +}; + test("이진수 문자열을 반환한다", () => { expect(solution(0)).toBe("0"); expect(solution(1)).toBe("1"); From e284eaaef864a138ae73b8fdfd62ccc9f52ac13c Mon Sep 17 00:00:00 2001 From: Eagerbeaver <86523545+YeongseoYoon@users.noreply.github.com> Date: Tue, 8 Aug 2023 18:24:22 +0900 Subject: [PATCH 08/15] solution: problem-4/solution-1 --- problem-4/problem-4.test.js | 43 +++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/problem-4/problem-4.test.js b/problem-4/problem-4.test.js index 19f5cb0..a558d86 100644 --- a/problem-4/problem-4.test.js +++ b/problem-4/problem-4.test.js @@ -1,21 +1,36 @@ -const solution = () => { +//1. 가장 익숙한 방법으로 문제를 해결해 주세요. +const solution = (n) => { + let decimal = 0; + let exponent = 0; + if (n === "0" || n === "1") { + return parseInt(n, 10); + } + const length = n.length; + while (exponent <= length - 1) { + decimal += parseInt(n.charAt(n.length - 1), 10) * 2 ** exponent; + + exponent++; + n = n.slice(0, -1); + } + return decimal; }; -test('10진수 숫자를 반환한다', () => { - expect(solution('0')).toBe(0); - expect(solution('1')).toBe(1); - expect(solution('10')).toBe(2); - expect(solution('11')).toBe(3); - expect(solution('100')).toBe(4); - expect(solution('101')).toBe(5); - expect(solution('110')).toBe(6); - expect(solution('111')).toBe(7); - expect(solution('1000')).toBe(8); +test("10진수 숫자를 반환한다", () => { + expect(solution("0")).toBe(0); + expect(solution("1")).toBe(1); + expect(solution("10")).toBe(2); + expect(solution("11")).toBe(3); + expect(solution("100")).toBe(4); + expect(solution("101")).toBe(5); + expect(solution("110")).toBe(6); + expect(solution("111")).toBe(7); + expect(solution("1000")).toBe(8); }); -test('큰 입력이 주어져도 RangeError를 던지지 않는다', () => { +test("큰 입력이 주어져도 RangeError를 던지지 않는다", () => { const input = Number.MAX_VALUE.toString(2); - expect(() => solution(input)) - .not.toThrowError(new RangeError('Maximum call stack size exceeded')); + expect(() => solution(input)).not.toThrowError( + new RangeError("Maximum call stack size exceeded") + ); }); From efd19ebeb440e6220a8a9440633bc3e7ce09f61b Mon Sep 17 00:00:00 2001 From: Eagerbeaver <86523545+YeongseoYoon@users.noreply.github.com> Date: Tue, 8 Aug 2023 18:42:41 +0900 Subject: [PATCH 09/15] solution: problem-4/solution-2 --- problem-4/problem-4.test.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/problem-4/problem-4.test.js b/problem-4/problem-4.test.js index a558d86..7bfb46e 100644 --- a/problem-4/problem-4.test.js +++ b/problem-4/problem-4.test.js @@ -1,4 +1,6 @@ //1. 가장 익숙한 방법으로 문제를 해결해 주세요. +{ + /* const solution = (n) => { let decimal = 0; let exponent = 0; @@ -13,6 +15,15 @@ const solution = (n) => { n = n.slice(0, -1); } return decimal; +};*/ +} +//2. 이번에는 재귀 함수로 문제를 해결해 주세요. +const solution = (n, acc = 0, exponent = 0) => { + if (n.length === 1) { + return acc + parseInt(n, 10) * 2 ** exponent; + } + acc += parseInt(n.charAt(n.length - 1), 10) * 2 ** exponent; + return solution(n.slice(0, -1), acc, exponent + 1); }; test("10진수 숫자를 반환한다", () => { From 721cb47b67a45b627a28b94a47237e10631b3850 Mon Sep 17 00:00:00 2001 From: Eagerbeaver <86523545+YeongseoYoon@users.noreply.github.com> Date: Tue, 8 Aug 2023 18:48:37 +0900 Subject: [PATCH 10/15] solution: problem-4/solution-3,4 --- problem-4/problem-4.test.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problem-4/problem-4.test.js b/problem-4/problem-4.test.js index 7bfb46e..b5b580c 100644 --- a/problem-4/problem-4.test.js +++ b/problem-4/problem-4.test.js @@ -18,12 +18,35 @@ const solution = (n) => { };*/ } //2. 이번에는 재귀 함수로 문제를 해결해 주세요. +{ + /* const solution = (n, acc = 0, exponent = 0) => { if (n.length === 1) { return acc + parseInt(n, 10) * 2 ** exponent; } acc += parseInt(n.charAt(n.length - 1), 10) * 2 ** exponent; return solution(n.slice(0, -1), acc, exponent + 1); +};*/ +} + +//3. 꼬리 재귀 함수로 바꿔보세요. +//4. 꼬리 재귀 최적화를 통해서 최적화해 보세요. +const solution = (n) => { + let acc = 0; + let exponent = 0; + + const length = n.length; + if (length === 1) { + return acc + parseInt(n, 10) * 2 ** exponent; + } + + while (exponent <= length - 1) { + acc += parseInt(n.charAt(n.length - 1), 10) * 2 ** exponent; + + exponent++; + n = n.slice(0, -1); + } + return acc; }; test("10진수 숫자를 반환한다", () => { From 08cc4bbc9f1799f74dfb73af84c29958e88f4e8e Mon Sep 17 00:00:00 2001 From: Eagerbeaver <86523545+YeongseoYoon@users.noreply.github.com> Date: Wed, 9 Aug 2023 13:06:31 +0900 Subject: [PATCH 11/15] solution: problem-5/solution-1,2 --- problem-5/problem-5.test.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/problem-5/problem-5.test.js b/problem-5/problem-5.test.js index 20a8fab..dd21e58 100644 --- a/problem-5/problem-5.test.js +++ b/problem-5/problem-5.test.js @@ -1,17 +1,23 @@ -const solution = () => { +//1. 가장 익숙한 방법으로 문제를 해결해 주세요. +const solution = (b, a) => { + if (a === 0) { + return b; + } + return solution(a, b % a); }; -test('최대 공약수를 반환한다', () => { +test("최대 공약수를 반환한다", () => { expect(solution(4, 12)).toBe(4); expect(solution(3, 7)).toBe(1); expect(solution(16, 72)).toBe(8); expect(solution(9, 12)).toBe(3); }); -test('큰 입력이 주어져도 RangeError를 던지지 않는다', () => { +test("큰 입력이 주어져도 RangeError를 던지지 않는다", () => { const a = Number.MAX_VALUE; const b = 1213; - expect(() => solution(a, b)) - .not.toThrowError(new RangeError('Maximum call stack size exceeded')); + expect(() => solution(a, b)).not.toThrowError( + new RangeError("Maximum call stack size exceeded") + ); }); From 92e324a1b99934a9918782cb13c2fca08827579e Mon Sep 17 00:00:00 2001 From: Eagerbeaver <86523545+YeongseoYoon@users.noreply.github.com> Date: Wed, 9 Aug 2023 13:06:50 +0900 Subject: [PATCH 12/15] solution: problem-5/solution-3,4 --- problem-5/problem-5.test.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problem-5/problem-5.test.js b/problem-5/problem-5.test.js index dd21e58..b8d4efe 100644 --- a/problem-5/problem-5.test.js +++ b/problem-5/problem-5.test.js @@ -1,10 +1,30 @@ //1. 가장 익숙한 방법으로 문제를 해결해 주세요. +//2. 이번에는 재귀 함수로 문제를 해결해 주세요. +{ + /* const solution = (b, a) => { if (a === 0) { return b; } return solution(a, b % a); }; +*/ +} + +//3. 꼬리 재귀 함수로 바꿔보세요. +//4. 꼬리 재귀 최적화를 통해서 최적화해 보세요. +const solution = (b, a) => { + let temp; + while (true) { + if (a === 0) { + return b; + } + temp = a; + a = b % temp; + b = temp; + console.log(b + " " + a); + } +}; test("최대 공약수를 반환한다", () => { expect(solution(4, 12)).toBe(4); From f0be6f7f3e03a24f2ff1cef13d76f9e727e00cfa Mon Sep 17 00:00:00 2001 From: Eagerbeaver <86523545+YeongseoYoon@users.noreply.github.com> Date: Wed, 9 Aug 2023 14:16:07 +0900 Subject: [PATCH 13/15] solution: problem-6/solution-1 --- problem-6/problem-6.test.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/problem-6/problem-6.test.js b/problem-6/problem-6.test.js index 059e2b8..ef4f84f 100644 --- a/problem-6/problem-6.test.js +++ b/problem-6/problem-6.test.js @@ -1,7 +1,21 @@ +//1. 재귀 함수로 문제를 해결해 주세요. const solution = (n) => { + if (n === 1) { + return 1; + } + + if (n === 2) { + return 2; + } + + if (n === 3) { + return 4; + } + + return solution(n - 1) + solution(n - 2) + solution(n - 3); }; -test('계단에 오를 수 있는 가지 수를 반환한다', () => { +test("계단에 오를 수 있는 가지 수를 반환한다", () => { expect(solution(1)).toBe(1); expect(solution(2)).toBe(2); expect(solution(3)).toBe(4); @@ -14,6 +28,6 @@ test('계단에 오를 수 있는 가지 수를 반환한다', () => { expect(solution(10)).toBe(274); }); -test('큰 입력이 주어져도 시간안에 실행된다', async () => { +test("큰 입력이 주어져도 시간안에 실행된다", async () => { expect(solution(40)).toBe(23837527729); }); From a0c053477b3653994c5ef88d54150b36914d7e37 Mon Sep 17 00:00:00 2001 From: Eagerbeaver <86523545+YeongseoYoon@users.noreply.github.com> Date: Wed, 9 Aug 2023 14:39:13 +0900 Subject: [PATCH 14/15] solution: problem-6/solution-2 --- problem-6/problem-6.test.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/problem-6/problem-6.test.js b/problem-6/problem-6.test.js index ef4f84f..8ba6b97 100644 --- a/problem-6/problem-6.test.js +++ b/problem-6/problem-6.test.js @@ -1,5 +1,6 @@ //1. 재귀 함수로 문제를 해결해 주세요. -const solution = (n) => { +{ + /*const solution = (n) => { if (n === 1) { return 1; } @@ -13,6 +14,29 @@ const solution = (n) => { } return solution(n - 1) + solution(n - 2) + solution(n - 3); +};*/ +} + +//2. 다이나믹 프로그래밍으로 최적화 해주세요. +const solution = (n, memo = []) => { + if (n === 1) { + return 1; + } + + if (n === 2) { + return 2; + } + + if (n === 3) { + return 4; + } + + if (!memo[n]) { + memo[n] = + solution(n - 1, memo) + solution(n - 2, memo) + solution(n - 3, memo); + } + + return memo[n]; }; test("계단에 오를 수 있는 가지 수를 반환한다", () => { From 605e4f346941efba9a2b61b95afa2266d6ea3efe Mon Sep 17 00:00:00 2001 From: Eagerbeaver <86523545+YeongseoYoon@users.noreply.github.com> Date: Wed, 9 Aug 2023 15:07:41 +0900 Subject: [PATCH 15/15] solution: problem-2/solution-3,4 --- problem-2/problem-2.test.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problem-2/problem-2.test.js b/problem-2/problem-2.test.js index dde4a23..e20a284 100644 --- a/problem-2/problem-2.test.js +++ b/problem-2/problem-2.test.js @@ -1,5 +1,7 @@ //1. 가장 익숙한 방법으로 문제를 해결해 주세요. //2. 이번에는 재귀 함수로 문제를 해결해 주세요. +{ + /* const solution = (n) => { if (n <= -1) { return 0; @@ -9,6 +11,27 @@ const solution = (n) => { return solution(n - 1) + solution(n - 2); } }; +*/ +} +//3. 꼬리 재귀 함수로 바꿔보세요. +//4. 꼬리 재귀 최적화를 통해서 최적화해 보세요. +const solution = (n) => { + let pre = 0; + let curr = 1; + let temp = 0; + if (n <= -1) { + return 0; + } else if (n === 0 || n === 1) { + return n; + } + while (n >= 2) { + temp = pre; + pre = pre + curr; + curr = temp; + n--; + } + return pre + curr; +}; test("음수가 주어지면 0을 반환한다", () => { expect(solution(-1)).toBe(0);