728x90 1281. Subtract the Product and Sum of Digits of an Integer1 1281. Subtract the Product and Sum of Digits of an Integer 문제 n의 요소를 하나씩 분리 후 곱한 결과와 더하기 한 결괏값을 빼기 후 그 값을 리턴하는 문제다. 내 현재 풀이 /** * @param {number} n * @return {number} */ var subtractProductAndSum = function(n) { n = n.toString(); n = n.split(''); let sum = n.reduce((a,c)=> a+Number(c),0) let product = n.reduce((a,c)=> a * Number(c),1) console.log(sum) return product - sum }; n이 처음에 int 형여서 split 함수를 사용하기 위해서 string 형으로 변환을 했다. 그다음 split를 사용해서 배열로 변환 후 r.. 2022. 6. 2. 이전 1 다음 728x90