-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- sum 함수를 재귀 함수로 변경하여 arr.length를 size 함수로 추출 - average 함수에서 sum 함수를 호출하도록 변경 - average 함수에 대한 테스트 코드 작성 및 검증
- Loading branch information
Showing
2 changed files
with
16 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
const average = (arr: number[]) => { | ||
let answer = 0; | ||
const size = (arr: number[]) => arr.length; | ||
|
||
for (let i = 0; i < arr.length; i++) { | ||
answer += arr[i]; | ||
} | ||
const sum = (arr: number[], index = 0) => (index === arr.length ? 0 : arr[index] + sum(arr, index + 1)); | ||
|
||
return answer / arr.length; | ||
}; | ||
// 높은 수준의 추상화과 낮은 수준의 코드이면 동일한 추상화를 한다. | ||
const average = (arr: number[], answer: number = 0) => sum(arr) / size(arr); | ||
|
||
console.log(average([1, 2, 3, 4, 5])); // 결과값 : 3 | ||
describe('average', () => { | ||
it('배열의 합의 평균값을 반환한다.', () => { | ||
expect(average([1, 2, 3, 4, 5])) | ||
.toBe(3); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
describe('sum', () => { | ||
it('두 수가 주어지면 합의 결괏값을 리턴해야 한다.', () => { | ||
expect(1 + 2).toBe(3); | ||
const sumValue = (a: number, b: number) => a + b; | ||
|
||
describe('sumValue', () => { | ||
it('두 수가 주어지면 합의 결과값을 리턴해야 한다.', () => { | ||
expect(sumValue(1, 2)) | ||
.toBe(3); | ||
}); | ||
}); |