N개의 자연수가 입력되면 각 자연수의 자릿수의 합을 구하고, 그 합이 최대인 자연수를 출력
하는 프로그램을 작성하세요. 자릿수의 합이 같은 경우 원래 숫자가 큰 숫자를 답으로 합니다.
만약 235 와 1234가 동시에 답이 될 수 있다면 1234를 답으로 출력해야 합니다.
| Input | Output | 
|---|---|
| [128, 460, 603, 40, 521, 137, 123] | 137 | 
const solution = (numbers) => {
    const maxCandidates = [];
    let maxSum = 0;
    let maxNum = 0;
    const numObjects = numbers.map((el) => {
        const str = el + '';
        const arr = str.split('');
        const sum = arr.reduce((acc, cur) => {
            return acc += Number(cur);
        }, 0);
        return ({ number: el, sum: sum })
    });
    
    numObjects.sort((a, b) => b.sum - a.sum);
    for (let i = 0; i < numObjects.length; i++) {
        if (numObjects[i].sum >= maxSum) {
            maxSum = numObjects[i].sum;
            maxCandidates.push(numObjects[i]);
        }
        else break;
    }
    maxCandidates.forEach((el) => {
        if (el.number > maxNum) maxNum = el.number;
    });
    
    return maxNum;
}