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. :/ ..

2

7 Answers 7

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);

Sign up to request clarification or add additional context in comments.

2 Comments

but I have an array dynamic, how to implement in this case..
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?
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

2 Comments

why is there a var inside of the loop?
I made an edit to fix it, there were several things wrong.
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([]));

Comments

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

Comments

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

Comments

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]));

Comments

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

2 Comments

yes.. thanks.. "<=" .. but acc = acc *items[i] I tried and don´t accumulate in var.
i update question - because have a dynamic array ("<=" dynamicArray.length) its i want to say...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.