카테고리 없음
프로그래머스 LV.0 자릿수 더하기
ctrs
2023. 9. 14. 13:21
프로그래머스 LV.0 자릿수 더하기
https://school.programmers.co.kr/learn/courses/30/lessons/120906
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 문제 명
자릿수 더하기
2. 문제 설명
정수 n이 매개변수로 주어질 때 n의 각 자리 숫자의 합을 return하도록 solution 함수를 완성해주세요.
3. 제한 사항
- 0 ≤ n ≤ 1,000,000
4. 예시
n | result |
1234 | 10 |
930211 | 16 |
5. 기본 제공 코드
function solution(n) {
var answer = 0;
return answer;
}
6. 제출한 내 답
const solution = (n) => {
let a = [...(n + "")].reduce((a, b) => {
return a + +b;
}, 0);
return a;
};
6-2. VSC에 작성한 내용
const solution = (n) => {
let a = [...(n + "")].reduce((a, b) => {
return a + +b;
}, 0);
return a;
};
// 테스트
console.log(solution(1234));
console.log(solution(930211));
7. 특이사항
없음
8. 다른 사람이 작성한 답
8-1. 한줄로
function solution(n) {
return n
.toString()
.split("")
.reduce((acc, cur) => acc + Number(cur), 0);
}