Skip to content
Prev Previous commit
Next Next commit
Update 히샤드 수.js
  • Loading branch information
jaewon1676 committed Apr 4, 2022
commit 0a26511d5bd5b5f5d5fd22582f8dc2590669e113
14 changes: 13 additions & 1 deletion level-1/히샤드-수.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,16 @@ function solution(x) {
}
// x가 각 자릿수의 합으로 나누어떨어지는지 여부 반환
return x % sum_of_digits == 0;
}
}

//정답 3 - jaewon1676
function solution(x) {
let sum = 0;
let arr = String(x).split(''); // 숫자를 하나씩 분리한다.

for(var i=0; i<arr.length; i++) {
sum += Number(arr[i]); // 각 숫자를 더해준다.
}

return (x % sum == 0) ? true : false; // 자릿수의 합으로 x가 나누어지면 하샤드 수
}