code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

1281 Subtract The Product And Sum Of Digits Of An Integer

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/*
 * Given an integer number n, return the difference between the product
 * of its digits and the sum of its digits.
 *
 *
 * Example 1:
 *
 * Input: n = 234
 * Output: 15
 * Explanation:
 * Product of digits = 2 * 3 * 4 = 24
 * Sum of digits = 2 + 3 + 4 = 9
 * Result = 24 - 9 = 15
 */

function subtractTheProductAndSumOfDigitsOfAnInteger(num) {
  let sum = 0
  let product = 1
  while (num > 0) {
    sum += num % 10
    product *= num % 10
    num = Math.floor(num / 10)
  }
  return product - sum
}

test('subtract The Product And Sum Of Digits Of An Integer', () => {
  //expect(subtractTheProductAndSumOfDigitsOfAnInteger(234)).toEqual(15)
  //expect(subtractTheProductAndSumOfDigitsOfAnInteger(4421)).toEqual(21)
  expect(subtractTheProductAndSumOfDigitsOfAnInteger(643)).toEqual(59)
})

Created 2020-04-18T23:32:39+00:00 · Edit