/*
https://school.programmers.co.kr/learn/courses/30/lessons/12931
์ž์—ฐ์ˆ˜ N์ด ์ฃผ์–ด์ง€๋ฉด, N์˜ ๊ฐ ์ž๋ฆฟ์ˆ˜์˜ ํ•ฉ์„ ๊ตฌํ•ด์„œ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์„ธ์š”.
์˜ˆ๋ฅผ๋“ค์–ด N = 123์ด๋ฉด 1 + 2 + 3 = 6์„ return ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ œํ•œ์‚ฌํ•ญ
N์˜ ๋ฒ”์œ„ : 100,000,000 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜
*/

const solution = (num) => {
  let answer = 0;

  // ์Šคํ”„๋ ˆ๋“œ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋ฌธ์ž์—ดํ™”
  const numArr = num.toString();
  // ์Šคํ”„๋ ˆ๋“œ ์—ฐ์‚ฐ์ž๋กœ ๋ฐฐ์—ด ๋ถ„ํ•ด
  const numStr = [...numArr];
  // ['1', '2', '3'], ['9', '8', '7']

  // for๋ฌธ์„ ๋Œ๋ ค
  for (let i = 0; i < numStr.length; i++) {
    answer += Number(numStr[i]); // ๋ถ„ํ•ด๋œ ๊ฐ ์š”์†Œ๋ฅผ ์ •์ˆ˜ํ™” ์‹œํ‚ค๊ณ  answer์— ๋”ํ•˜๊ธฐ
  }

  return answer;
};

console.log(solution(123));
console.log(solution(987));

์Šคํ”„๋ ˆ๋“œ ์—ฐ์‚ฐ์ž๋กœ ๋น„๊ต์  ์‰ฝ๊ฒŒ ํ’€์–ด๋ƒˆ๋‹ค.

๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค!