1

The best and the easiest way to multiply array numbers sequentially

I have got an array with some values:

    const arr = [1, 5, 12, 3, 83, 5];

Now I want to get the product of all values from arr. It should works like this: 1 * 5 * 12 * 3 * 83 * 5

I have tried with this code:


    const arr = [1, 5, 12, 3, 83, 5];

    multiply(arr);

    function multiply(arr) {
        for(i = 0; i < arr.length; i++) {
            product = array[i] * array[i];
            console.log(product);
        }
    }

This code above works like this: 1 * 1, 5 * 5, 12 * 12, 3 * 3, 83 * 83, 5 * 5 and that is not result that I need. I think I know why it works like this but I'm not sure how to write code that I need.

So what's the best option for this kind of tasks?

Edit. For non-experienced people looking here in future this is the best option that we've found:

Leo Martin answer:

    const array = [1, 5, 12, 3, 83, 5];

    console.log(multiply(array)); // we log function return value

    function multiply(array) {
      let score = array[0];
      for (i = 1; i < array.length; i++) {
        score = score * array[i];
      }
      return score;
    }

and also the shorter version:

By the way, you could use Array.reduce:


    const array = [1, 5, 12, 3, 83, 5];

    const result = array.reduce((acc, value, index) => {  
      if (index === 0) return value;
      acc = acc * value;

      return acc;
    }, 0);

    console.log(result);

3
  • can you do the sum instead of the product? Commented Jan 24, 2020 at 11:27
  • what do you mean by "console sum" ? Commented Jan 24, 2020 at 11:28
  • you seem to believe that array[i]*array[i] would magically store the product of all the values. All what yo udo is multiply each value by itself, the line will be executed each time with some i, the same i will be used at each iteration. Commented Jan 24, 2020 at 11:28

9 Answers 9

3

Just move console.log outside for body:

const array = [1, 5, 12, 3, 83, 5];
console.log(multiply(array)); // we log function return value
function multiply(array) {
  let score = array[0];
  for (i = 1; i < array.length; i++) {
    score = score * array[i];
  }
  return score;
}

By the way, you could use Array.reduce:

const array = [1, 5, 12, 3, 83, 5];

const result = array.reduce((acc, value, index) => {
  if (index === 0) return value;
  acc = acc * value;

  return acc;
}, 0);

console.log(result);

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

Comments

1

You can use reduce method to multiply all numbers

const array = [1, 5, 12, 3, 83, 5];
const total = array.reduce((total,num) => total * num, 1);
console.log(total)

Comments

1

Corrected your program.

    const array = [1, 5, 12, 3, 83, 5];
    multiply(array);
    function multiply(array) {
    	  var score = 1; 
        
        for(i = 0; i < array.length; i++) {
            score = score * array[i];        
        }
        console.log(score);
    }

	

2 Comments

thanks, it works. So var score = 1 is the first value of my array and then in for loop code multiply it by first value so score = 1, next multiply it by 5 so score = 5, next multiply it by 12 et cetera. Do i understand it ok?
Yeah! Correct. There can be many better ways but this one is more understandable.
0

Hope the solution is self explanatory. Loop through array. Multiply each product and store the result in the same variable.

const array = [1, 5, 12, 3, 83, 5];
multiply(array);
function multiply(array) {
    let product = 1;
    for(i = 0; i < array.length; i++) {
        product *= array[i];
    }
    console.log(product);
}

Comments

0

const array = [1, 5, 12, 3, 83, 5];
multiply(array);
function multiply(array) {
    var score = array[0];
    for(i = 1; i < array.length; i++) {
        score = score * array[i];
    }
    console.log(score);
}

Comments

0

var array = [1, 5, 12, 3, 83, 5];

var result = array.reduce(function(a, b) {
  return a * b;
});

console.log(result);

2 Comments

Array.reduce needs an initial value. You're multiplying a Number by undefined
@LéoMartin if no initial value is passed to .reduce() it will set the accumulator to the first element and begin iterating from the second element. This can be problematic when the array is empty though
0
const array = [1, 5, 12, 3, 83, 5];
multiply(array);
function multiply(array) {
    for(i = 0; i < array.length-1; i++) {
        score = array[i] * array[i+1];
        console.log(score);
    }
}

1 Comment

From Review: Welcome to Stack Overflow! Please don't answer just with source code. Try to provide a nice description about how your solution works. See: How do I write a good answer?. Thanks
0

For the calculation of the final result of multiply:

    const array = [1, 5, 12, 3, 83, 5];
    multiply(array);
    function multiply(array) {
      score = 1;
      for(i = 0; i < array.length; i++) {
        score = score * array[ i ];
      }
      console.log(score); // expected result: 74700
    }

Are you asking for a textual representation of the calculations? If so, use this:

const array = [1, 5, 12, 3, 83, 5];
    multiply(array);
    function multiply(array) {
      score = '';
      for(i = 0; i < array.length; i++) {
        score += array[i].toString();
        if ( i < array.length -1  ) score += '*';
      }
      console.log(score); // expected result: '1*5*12*3*83*5'
    }

This should start to put you on the right way, hope it helps.

Comments

0

score = array[i] * array[i]; you are trying to multiply exact exp-rations.

 const array = [1, 5, 12, 3, 83, 5];

its like: 1*1, 5*5

you can multiply array[i] * i

another approach might be an array function.

    const array = [1, 5, 12, 3, 83, 5];
    multiply(array);
    function multiply(array) {
        let product = 1;
        `enter code here`for(i = 0; i < array.length; i++) {
            product *= array[i];
        }
        console.log(product);
    }

let multiArray = array.map((num, index) => num *index)
console.log(multiArray)

for more info:https://developer.mozilla.org/enUS/docs/Web/JavaScript/Reference/Global_Objects/Array/map.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.