Skip to content

Solve : 046번 문제 해결 #259

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

Merged
merged 1 commit into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Challenge/YurinWang/046.각_자리수_합2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 문제46 : 각 자리수의 합 2

1부터 20까지의(20을 포함) 모든 숫자를 일렬로 놓고 모든 자릿수의 총 합을 구하세요.

예를 들어 10부터 15까지의 모든 숫자를 일렬로 놓으면 101112131415이고
각 자리의 숫자를 더하면 21입니다. (1+0+1+1+1+2+1+3+1+4+1+5 = 21)
24 changes: 24 additions & 0 deletions Challenge/YurinWang/046.각_자리수_합2/solve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
# 문제46 : 각 자리수의 합 2

1부터 20까지의(20을 포함) 모든 숫자를 일렬로 놓고 모든 자릿수의 총 합을 구하세요.

예를 들어 10부터 15까지의 모든 숫자를 일렬로 놓으면 101112131415이고
각 자리의 숫자를 더하면 21입니다. (1+0+1+1+1+2+1+3+1+4+1+5 = 21)
*/

let arr = [];
let sum = 0;

for (let i = 0; i < 20; i++) {
arr[i] = i + 1;
}

arr.forEach((n) => {
while (n !== 0) {
sum += n % 10;
n = Math.floor(n / 10);
}
});

console.log(sum);