1

I have dynamic input of items 0 to N in a function (JavaScript).

In each position have value like items[1,3,4], creating a loop:

update: items or "the array" is dynamic I don't have permanent values.

ex.: for (var i = 0;i < items.length; i++){}

Need return the multiplication of all items, only know the total of array (items.length) inside of "for".

So.. 1st. need be cumulative

2nd maybe multiply each inside a loop item[value] * item[value] *

3rd maybe if(i == items.length){return total} (ex.: 1*3*4 = 12)

dont know how to cumulative var total = item[value] * item[value] * ...

function **multiplyEach**(item){
  item[1,3,4]; //only ex.:this array have many possibilities of length
  for( var i = 0; i <= item.length; i++){
    var **items** = item[i] * item[i]; 
    if (i == item.length){
      return items;
    }
  }
}

items have NaN or undefined value in debug of Chrome browser. :/ ..

user1581986
  • 11
  • 1
  • 4
  • 3
    array reduce() method – epascarello Jul 24 '19 at 19:58
  • 1
    Possible duplicate of [How to find the sum of an array of numbers](https://stackoverflow.com/questions/1230233/how-to-find-the-sum-of-an-array-of-numbers) or [Multiply all elements in array](https://stackoverflow.com/q/19175063/215552) – Heretic Monkey Jul 24 '19 at 20:17

7 Answers7

2

I guess this is what you want...

var items = [1,3,4]; //only ex.:this array have many possibilities of length

var product = 1;
for( var i = 0; i < items.length; i++){
  product *= items[i]; 
}

// product === 12
Potter
  • 633
  • 7
  • 13
Yogeshwar Singh
  • 1,245
  • 1
  • 10
  • 22
2

As @epascarello suggested you can use Array.reduce() function to achieve this:

const multiplyEach = nums => nums.reduce((res, num) => res * num, nums.length ? 1 : 0);

console.log('multiplyEach([1,3,4]) = ', multiplyEach([1,3,4]));
console.log('multiplyEach([]) = ', multiplyEach([]));
Andriy
  • 14,781
  • 4
  • 46
  • 50
2

This is a good use-case for Array.prototype.reduce

const product = [1, 3, 4].reduce((product, n) => product * n, 1);

console.log(product);
Tom O.
  • 5,730
  • 2
  • 21
  • 35
  • but I have an array dynamic, how to implement in this case.. – user1581986 Jul 24 '19 at 20:36
  • This is just an example of how someone would implement something like this - it's not really to copy and paste directly into your code. How do you think this code should be modified to handle a "dynamic" array? Maybe create a variable to reference the array and then `reduce` on that array, right? – Tom O. Jul 24 '19 at 20:41
1

There is an example of the array reduce function available to you in Javascript.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

chonerman
  • 117
  • 11
1

As others suggested, make use of the Array.prototype.reduce method. Documentation can be found at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

// This is an example for your use case.
const arr = [1,3,4];
let total = arr.reduce((a, b) => a * b);
console.log(total); // 12
0000
  • 11
  • 4
0

Start the product with an initial value of 1. Store it in a separate variable. Multiply each item with it. Do the return after the loop, not conditionally inside the loop. Don't let your loop run til item.length, but item.length-1 which is the last index.

function multiplyEach(items) {
    var product = 1;
    for (var i = 0; i < items.length; i++) {
        product = product * items[i];
    }
    return product;
}
console.log(multiplyEach([1,3,4]));
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

Typically when you do something like this, you have some variable outside of the for-loop that keeps track of your current "accumulating" value. Then, for each item in the array, update your variable. Here's a quick example:

function multiplyEach(items){
    // variable we will use to keep track of the current value
    var acc = 1;
    // now loop through the items
    for( var i = 0; i < items.length; i++ ){
        // now we update our variable
        acc = acc * items[i]; 
    }
    // now return our value
    return acc;
}

One more thing to note, is in your example you had:

for( var i = 0; i <= item.length; i++)

This will throw an error, since you're iterating one time too far. You have "<=" instead of "<", which means if you have 3 items, your loop will count: 0,1,2,3