Project Euler 8. Feedback request

In this solution, I used some check with indexOf method in order to avoid some multiplications with reduce() method.

Logic was: why bother with multiplications: if we have zeroamong multiplicators, product will be zero anyway. Not sure if it was necessary??

I removed from code below 1000-digit number for readability :upside_down_face: It can be found in challenge step.

  function largestProductinaSeries(n) {

   /* let thousandDigits = here should be 1000-element array ;*/
    let theLargestProduct = 0;

    for (let i = 0; i <= thousandDigits.length - n; i++) {

        const testedChunk = thousandDigits.slice(i, i + n);
        //way to optimize: if zero is present in testedChunk, can 'leap over' some loopsteps
        //depending on indexOf(i)
        if (testedChunk.indexOf(0) === -1) {
        //need to declare testedProduct in order not to use reduce twice    
            const testedProduct = testedChunk.reduce(
                (previous, current) => previous * current, 1
            );           
            if (testedProduct > theLargestProduct) {
                theLargestProduct = testedProduct;
            }
        }
    }

    return theLargestProduct;
  }
  
  largestProductinaSeries(13);

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.