leetCode 문제 풀이 1281번 Subtract the Product and Sum of Digits of an Integer

devmomo·2021년 3월 11일
0

알고리즘

목록 보기
16/52
post-thumbnail

1281. Subtract the Product and Sum of Digits of an Integer

문제
정수로 이루어진 배열 n이 주어질 때, 조건을 만족하는 값을 return 하는 함수 짜기

가정
n은 1이상 100,000이하의 정수

조건
n의 각 자리의 숫자들을 추출하고, 그 수들의 곱과 합의 차를 출력

풀이

var subtractProductAndSum = function(n) {
    const result = [...String(n)].map((data)=>parseInt(data,10));
    const pDigit = result.reduce((a,b)=>a*b);
    const sDigit = result.reduce((a,b)=>a+b);
    return pDigit - sDigit;
};
profile
FE engineer

0개의 댓글