Skip to content

Commit f217ad8

Browse files
authored
20251205 (#32)
add: 자리수의 합
1 parent a8e880b commit f217ad8

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function solution(nums) {
2+
let answer = 0;
3+
let max = Number.MIN_SAFE_INTEGER;
4+
5+
for (const num of nums) {
6+
let temp = num;
7+
let sum = temp % 10;
8+
9+
while (temp > 0) {
10+
sum += temp % 10;
11+
temp = Math.floor(temp / 10);
12+
}
13+
14+
if (sum > max) {
15+
max = sum;
16+
answer = num;
17+
} else if (sum === max) {
18+
answer = Math.max(answer, num);
19+
}
20+
}
21+
22+
return answer;
23+
}
24+
25+
console.log(solution([128, 460, 603, 40, 521, 137, 122])); // 137
26+
console.log(solution([5, 3, 7, 11, 2, 15, 17])); // 17

0 commit comments

Comments
 (0)