diff --git a/problem-1/problem-1.test.js b/problem-1/problem-1.test.js index c775a02..2188a95 100644 --- a/problem-1/problem-1.test.js +++ b/problem-1/problem-1.test.js @@ -1,13 +1,27 @@ -const solution = (numbers) => { +const solution = (numbers) => (numbers.length ? numbers.reduce((acc, curr) => acc + curr) : 0); + +const solution2 = (numbers) => { + let acc = 0; + + while (numbers.length > 0) { + acc += numbers.pop(); + } + + return acc; }; test('빈 배열은 0을 반환한다', () => { expect(solution([])).toBe(0); + + expect(solution2([])).toBe(0); }); test('배열의 합을 반환한다', () => { expect(solution([1, 2, 3, 4])).toBe(10); expect(solution([-1, 3, 8, 9, 10, 11])).toBe(40); + + expect(solution2([1, 2, 3, 4])).toBe(10); + expect(solution2([-1, 3, 8, 9, 10, 11])).toBe(40); }); test('큰 배열이 입력으로 주어져도 RangeError를 던지지 않는다', () => { @@ -15,4 +29,7 @@ test('큰 배열이 입력으로 주어져도 RangeError를 던지지 않는다' expect(() => solution(input)) .not.toThrowError(new RangeError('Maximum call stack size exceeded')); + + expect(() => solution2(input)) + .not.toThrowError(new RangeError('Maximum call stack size exceeded')); }); diff --git a/problem-2/problem-2.test.js b/problem-2/problem-2.test.js index 458a6b8..e165589 100644 --- a/problem-2/problem-2.test.js +++ b/problem-2/problem-2.test.js @@ -1,4 +1,20 @@ const solution = (n) => { + if (n < 0) return 0; + if (n < 2) return n; + + let current = 2; + let a = 0; + let b = 1; + let sum = a + b; + + while (current < n) { + current += 1; + a = b; + b = sum; + sum = a + b; + } + + return sum; }; test('음수가 주어지면 0을 반환한다', () => { diff --git a/problem-3/problem-3.test.js b/problem-3/problem-3.test.js index 7319eb8..cc449f4 100644 --- a/problem-3/problem-3.test.js +++ b/problem-3/problem-3.test.js @@ -1,4 +1,18 @@ const solution = (n) => { + if (n === 0) { + return '0'; + } + + let memo = ''; + let number = n; + + while (number > 1) { + const remainder = number % 2; + memo = remainder + memo; + number = Math.floor(number / 2); + } + + return `1${memo}`; }; test('이진수 문자열을 반환한다', () => { diff --git a/problem-4/problem-4.test.js b/problem-4/problem-4.test.js index 19f5cb0..cf6a982 100644 --- a/problem-4/problem-4.test.js +++ b/problem-4/problem-4.test.js @@ -1,5 +1,11 @@ -const solution = () => { -}; +const solution = (binNum) => binNum + .split('') + .reverse() + .map((x, idx) => { + if (x === '0') { return 0; } + return (2 ** idx); + }) + .reduce((acc, curr) => acc + curr); test('10진수 숫자를 반환한다', () => { expect(solution('0')).toBe(0); diff --git a/problem-5/problem-5.test.js b/problem-5/problem-5.test.js index 20a8fab..479fdf0 100644 --- a/problem-5/problem-5.test.js +++ b/problem-5/problem-5.test.js @@ -1,4 +1,6 @@ -const solution = () => { +const solution = (a, b) => { + const remainder = a % b; + return remainder === 0 ? b : solution(b, remainder); }; test('최대 공약수를 반환한다', () => { diff --git a/problem-6/problem-6.test.js b/problem-6/problem-6.test.js index 059e2b8..5949cc1 100644 --- a/problem-6/problem-6.test.js +++ b/problem-6/problem-6.test.js @@ -1,4 +1,22 @@ const solution = (n) => { + let a = 1; + if (n === 1) return a; + + let b = 2; + if (n === 2) return b; + + let count = 3; + let result = 4; + + while (count < n) { + const temp = result; + result = a + b + result; + a = b; + b = temp; + + count += 1; + } + return result; }; test('계단에 오를 수 있는 가지 수를 반환한다', () => {