0

I have function taken from this example here that works well, except does not address any zeros that may be in the number, so everything is equaling zero when executing the function.

Multiplying individual digits in a number with each other in JavaScript

    function digitsMultip(data) {

      let arr = [];
    
      for (let i of data) {

       if (data[i] === 0) {

         arr.push(data[i]);

       }
     
      }
    
     return [...data.toString()].reduce((p, v) => p * v);

    };
    
console.log(digitsMultip(3025));

I added to it a for-loop that accounts for the zero and remove it, but im doing something wrong here.

Uncaught TypeError: data is not iterable

DESIRED OUTPUT

3025 => 3 * 2 * 5 = 30

georg
  • 211,518
  • 52
  • 313
  • 390
  • Correct me if I'm wrong but any number that contains a zero should output a zero? Because 1203 would be 1x2x3x0 which is 0. Or do you want to specifically exclude zeros so that it would become 1x2x3 = 6? – Matt Davis Sep 27 '19 at 22:23
  • @Matt yes, i want to exclude any zeros so that it would equal the latter example –  Sep 27 '19 at 22:25

3 Answers3

0

This iterates over the characters in your number. If the character is not "0" then it is added to the array. This array is then reduced by multiplying the values and then returned.

function digitsMultip(data) {

  const arr = [];

  for(let number of String(data)) {
     if (number !== "0")
       arr.push(number);
  }  

  return arr.reduce((p, v) => p * v);
};
    
console.log(digitsMultip(3025));
rcoro
  • 326
  • 6
  • 12
Matt Davis
  • 1,167
  • 8
  • 21
  • i understand the need to convert to string, but is there no need to convert back to a number? –  Sep 27 '19 at 22:42
  • 1
    Further type conversion is performed within the reduce function. So when reduce function attempts to do p * v for each number it will attempt "3" * "7" for example, but in JavaScript this is automatically converted to 3*7 due to type conversion. If you wanted you could, it would result in a number array being passed into the reduce function. The reduce function then wouldn't need to perform type conversion as you had already done it. – Matt Davis Sep 27 '19 at 22:48
  • 1
    Yes, you're precisely correct. The type conversion is performed implicitly. – Matt Davis Sep 27 '19 at 22:58
  • Word, appreciate the solution and explantion –  Sep 27 '19 at 23:00
0

You are getting that error because you are trying to iterate over a number. Passing in a string or converting the number to string before iterating it would make it work.

Instead of looping it that way, a better and readable way would be to use the filter method to filter out the chars before multiplying:

    function digitsMultip(data) {
     return [...data.toString()].filter(n => n > '0').reduce((p, v) => p * v);
    };
    
console.log(digitsMultip(3025));
Ricardo Souza
  • 16,030
  • 6
  • 37
  • 69
  • ah i gotcha, how does it add up and multiply strings? Is there some sort of coercion going on? How come there isn't a need to convert the string back to a number? –  Sep 27 '19 at 22:41
  • 1
    Yep, there are a lot of implicit conversions in JS. In this case, I convert the number to string and them spread it to an array of strings, then filter them based on string comparison. After that, I count on implicit conversions to do their work on multiplication inside of `reduce`. The first argument in reduce is the `accumulator`, that gets passed on in each iteration with the returned value of previous iterations. You can also provide a second parameter to `reduce` and that would be used as the starting value. – Ricardo Souza Sep 29 '19 at 22:39
0

Turn the input into a string, then split, filter the zeros and reduce multiplying

const input = 1203

const removeZeros = number =>{
  const arr = number.toString().split('').filter(i => i !== '0')
  
  return arr.reduce((a,c) => parseInt(a) * parseInt(c))
}
console.log(removeZeros(input))

One line version

const removeZeros = n => [...n.toString()].filter(c => c !== '0').map(x => parseInt(x)).reduce((a,c) => a*c)
Dupocas
  • 20,285
  • 6
  • 38
  • 56