Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

96
Views
My browser lags when I try a loop function?

I wrote a simple nested loop function to multiply all items in an array and output the total value, but each time is I run a loop function my browser either crashes or doesn't stop loading

function multiplyAll(arr){ 
        Let product = 1;
       for(let i = 0; i < 
            arr.length; i++){
            for(let j = 0; j < 
     arr[i].length; j *= product);
     }
      return product;
}

multiplyAll([[1], [2], [3]]);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You are creating an infinite loop here because of

for (let j = 0; j < arr[i].length; j *= product);

Here, j is always 0.

If you want to multiply all the nested value then you should do as:

function multiplyAll(arr) {
  let product = 1;
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr[i].length; ++j)
      product *= arr[i][j];
  }
  return product;
}

console.log(multiplyAll([[1], [2], [3]]));


If you just want to multiple all the nested value then you can simply do as:

function multiplyAll(arr) {
  return arr.flat().reduce((acc, curr) => acc * curr, 1);
  // If you want to get numbers at any depth then you can flat at any level
  // using Infinity as
  // return arr.flat(Infinity).reduce((acc, curr) => acc * curr, 1);
}

console.log(multiplyAll([[1], [2], [3]]));

about 4 years ago · Juan Pablo Isaza Report

0

Well, it looks like you have multiple issues, two of them are already as following:

  1. In line 2 you wrote once Let with a big letter at the beginning.
  2. Your second array needs an {} otherwise it will error out
  3. Can you please explain to me j*= product? Can you maybe include a few examples of how you would call this Code and what kind of response you would get? depending on that I will create you a Clear answer!
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!