function solution(num) { let sum = 0; const newStr = num.toString(); for (let i = 0; i < newStr.length; i++) { sum += Number(newStr[i]); } return sum; } console.log(solution(123)); // 6 console.log(solution(987)); // 24