Simple Solution | Beginner Friendly | Easy Approach with Explanation✅
Intuition\n Describe your first thoughts on how to solve this problem. \nTo solve the problem, we need to calculate two values:\n1. The product of the digits of
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo solve the problem, we need to calculate two values:\n1. The product of the digits of the number.\n2. The sum of the digits of the number.\nFinally, subtract the sum from the product. A straightforward solution involves converting the number into a string to process each digit individually.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Convert the number `n` into a string to easily access its digits.\n2. Initialize two variables: `sum` to store the sum of digits and `product` to store the product of digits. Set `sum` to 0 and `product` to 1.\n3. Loop through each character of the string representation of `n`:\n   - Convert the character to an integer using `parseInt()`.\n   - Add the digit to `sum`.\n   - Multiply the digit with `product`.\n4. Subtract `sum` from `product` to get the final result.\n5. Return the result.\n\n# Code\n```javascript []\n/**\n * @param {number} n\n * @return {number}\n */\nvar subtractProductAndSum = function(n) {\n    n = n.toString();\n    let sum=0;\n    let product =1;\n    for(let i=0;i<n.length;i++){\n        sum +=parseInt(n[i]);\n        product *=parseInt(n[i]);\n    }\n\n    let result =product-sum ;\n\n  \xA0\xA0return\xA0result;\n};\n```\n\n```typescript []\nfunction subtractProductAndSum(n: number): number {\n    const digits = n.toString();\n    let sum = 0, product = 1;\n    for (let i = 0; i < digits.length; i++) {\n        const digit = parseInt(digits[i]);\n        sum += digit;\n        product *= digit;\n    }\n    return product - sum;\n}
['TypeScript', 'JavaScript']