반응형
Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

undefined

[프로그래머스] 하샤드의 수 - JS 본문

Coding Test

[프로그래머스] 하샤드의 수 - JS

JavaScripter 2022. 5. 19. 23:42
반응형

문제 설명


문제 풀이

function solution(x) {
    let arr = Array.from(`${x}`).map(Number)
    let total = 0
    for(element of arr) {
        total = total += element
    }
    return x % total ? false : true
}

1.from으로 배열화 이후 map을 이용하여 숫자 배열로 변경

 

2. for of Loop이용하여 내부 배열 요소들의 총 합을 구함

 

3. 하샤드의 수인지 판별


개선 사항

반응형
Comments