-
Notifications
You must be signed in to change notification settings - Fork 21
1주차 과제 제출 #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
1주차 과제 제출 #16
Conversation
hannut91
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
고생 많으셨습니다 ㅎㅎ 재귀적인 문제를 잘 해결 하는방법은 반복적인 일로 볼 수 있는 안목을 길러야 합니다. 앞으로도 재귀적인 문제를 많이 만날탠데 1주차 때 연습했던 것을 많이 떠올려보면 좋겠습니다
| const solution = (numbers) => { | ||
| if (!numbers.length) return 0; | ||
| // 1. 가장 익숙한 방법으로 문제를 해결해 주세요. | ||
| const solution1 = numbers.reduce((prev, cur) => prev + cur, 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
꼬리재귀로 작성된 코드는 모두 reduce를 사용한 코드로 변환할 수 있습니다. 이전 값을 다음 값으로 넘기는 부분이 꼬리 재귀랑 똑같죠.
그리고 reduce에 파라미터 이름은 누산기(accumulator)라는 이름으로 acc를 자주 사용합니다.
numbers.reduce((acc, cur) => acc + cur, 0);| const solution3 = () => { | ||
| let sum = 0; | ||
| let arr = [...numbers]; | ||
| while (true) { | ||
| if (!arr.length) return sum; | ||
| const first = arr[0]; | ||
| sum += first; | ||
| arr = arr.slice(1); | ||
| } | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const solution3 = () => {
let sum = 0;
let arr = [...numbers];
let first;
while (true) {
if (arr.length === 0) {
return sum;
}
[first, ...arr] = arr;
sum += first;
}
};자바스크립트의 구조 분해 할당을 사용하면 이렇게도 할 수 있겠네요
| const temp = a; | ||
| a = b; | ||
| b = temp + b; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
요것도 구조 분해 할당을 사용하면 좀 더 간단하게 표현해볼 수 있겠어요
[a, b] = [b, a + b]|
아하 구조분해할당에 대해서도 더 알아가네요 ! 감사합니다 |
문제를 모두 풀지는 못했습니다!
우선 꼬리재귀함수를 이해하는 부분에서 시간이 좀 걸렸고 꼬리재귀함수를 반복문으로 최적화하는 부분은 강사님이 말씀 주신 것처럼 파라미터를 변수로 할당하는 것으로 기계적으로 할 수 있었습니다
근데 제가 문제 접근법이 어색해서 그런지 문제풀이 영상을 보면서 해결한 부분이 많았고, 그러다보니 시간이 좀 많이 걸려서 우선은 제출하겠습니다
2주차엔 보다 더 성실히 임하겠습니다ㅠ_ㅠ