Last active
March 29, 2021 10:40
-
-
Save jjori-master/9da6e4295a17f9153c674ab68e865c3d to your computer and use it in GitHub Desktop.
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
if(1) { | |
// 1차월 배열의 합- 재귀 | |
const _sum = (arr, idx) => { | |
if(idx == 0) { | |
return arr[0]; | |
} | |
return arr[idx] += _sum(arr, (idx - 1)) | |
} | |
const sum = (arr) => { | |
if(!arr instanceof Array || arr.length === 0) { | |
return 0; | |
} | |
const idx = arr.length - 1; | |
return _sum(arr, idx); | |
} | |
// 55 | |
sum([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) | |
} | |
if(1) { | |
// 1차월 배열의 합- 꼬리 재귀 | |
const _sum = (arr, idx, acc) => { | |
if(idx > 0) { | |
return _sum(arr, (idx - 1), (acc += arr[idx])) | |
} | |
return acc += arr[0]; | |
} | |
const sum = (arr) => { | |
if(!arr instanceof Array || arr.length === 0) { | |
return 0; | |
} | |
return _sum(arr, (arr.length - 1), 0); | |
} | |
// 55 | |
sum([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) | |
} | |
if(1) { | |
// 1차월 배열의 합- 번역한 for 문 | |
const sum = (arr) => { | |
if(!arr instanceof Array || arr.length === 0) { | |
return 0; | |
} | |
let acc = 0; | |
for (let idx = (arr.length - 1); idx > 0; idx--) { | |
acc += arr[idx]; | |
} | |
acc += arr[0]; | |
return acc; | |
} | |
// 55 | |
sum([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment