코딩테스트 연습

028-하샤드 수

__g__ 2022. 5. 18. 11:33
function solution(x) {
    x += ""		// x에 문자열을 더해 string으로 변환
    var total = 0;	// 자릿수의 합을 더할 변수
    

    for (let i = 0; i < x.length; i++) {   // x의 길이만큼 반복
        total += Number(x[i])
    }
   
    if (x % total == 0) {   // x가 total로 나누어 떨어진다면 true 반환
        return true
    } else {
        return false
    }

}