프로그래머스 | 자릿수 더하기 javascript

leehyuna·2022년 3월 13일
0

programmers

목록 보기
1/8
post-thumbnail

👩🏻‍💻 JS coding test | 알고리즘 Level1

💻 작성코드 1

javascript | parseInt
parseInt(string)
parseInt(string, radix)
string 문자열
radix 진수를 나타내는 2~36까지의 정수

function solution(n){
    let num = 0
    let stringNum = n.toString().split("")
    for (let i = 0; i < stringNum.length; i++) {
        num += parseInt(stringNum[i])
    }
    return num
}

💻 작성코드 2

javascript | forEach
Array에서만 사용 가능한 반복문
forEach((item, index) => element[index])
item 반복값
index 인덱스

function solution(n){
    let num = 0
    let stringNum = n.toString().split("")
    stringNum.forEach((ele, i) => num = num + + ele)
    return num
}
profile
seize the day ☺︎

0개의 댓글