Sum of Digits

Problem

Write a program that, when N natural numbers are input, finds the sum of the digits of each natural number, and outputs the natural number with the largest sum. If the sum of digits is the same, the answer is the number with the larger original number.
If 235 and 1234 can be the answer at the same time, you should output 1234 as the answer.

input

The number of natural numbers N (3<=N<=100) is given in the first line, and N natural numbers are given in the next line. The size of each natural number does not exceed 10,000,000.

7
128 460 603 40 521 137 123

output

Returns the natural number with the largest sum of digits.

137

Solution

using sort()

function solution(n, arr) {
	let answer,
		max = Number.MIN_SAFE_INTEGER;

	arr.sort((a, b) => a - b);

	for (let i of arr) {
		let temp = 0;

		for (let x of String(i)) {
			temp += parseInt(x);
		}

		if (temp >= max) {
			max = temp;
			answer = i;
		}
	}

	return answer;
}

not using sort()

function solution(n, arr) {
	let answer,
		max = Number.MIN_SAFE_INTEGER;

	for (let x of arr) {
		let sum = 0,
			tmp = x;
		while (tmp) {
			sum += tmp % 10;
			tmp = Math.floor(tmp / 10);
		}
		if (sum >= max) {
			max = sum;
			answer = x;
		} else if (sum === max) {
			if (x > answer) answer = x;
		}
	}

	return answer;
}