function solution(n, arr) {
let answer
let max = 0;
for(let i of arr){
let sum=0
let temp=i
while(temp){
sum+=(temp%10)
temp=Math.floor(temp/10)
}
if(sum>max){
max=sum;
answer = i
}else if(sum===max){
if(answer<i){
answer = i
}
}
}
return answer;
}
let arr = [128, 460, 603, 40, 521, 137, 123];
console.log(solution(7, arr));
for(let i of arr){ let sum=0 let temp=i while(temp){ sum+=(temp%10) temp=Math.floor(temp/10) }