Project Euler Problems 1 to 100 - Problem 32: Pandigital products

Tell us what’s happening:

I do not understand this reason why this does not work here. It works at my Visual Studio Code app.

Your code so far

function pandigitalProducts(n) {

  let base=10;
let limit=100;
let a=39;
let b=186;
let jointNumbers = 0;
let arrayOfNumbers=[];
let elementsOfAString=[];
let arrayOfNumbersFrom1toN=[];
let sortedArray=[];
let comparisonArray=[];
let arrayOfProducts=[];
let uniq = [];
let summatoryOfElementsOfAnArray=0;

for(let i =1;i<n+1;i++){
     comparisonArray.push(i)
};

for(let i = 1;i<2000;i++){
     for(let j = 1;j<2000;j++){
             a = i;
             b = j;
             if(a>=b){
                    arrayOfNumbers.push(a,b,(a*b))
                          
             jointNumbers= Number(arrayOfNumbers.join("")) 
           elementsOfAString = jointNumbers.toString().split('');
           arrayOfNumbersFrom1toN = elementsOfAString.map(Number)
           sortedArray=arrayOfNumbersFrom1toN.sort();
          // console.log("Comparison Array: ",comparisonArray)
          // console.log("Sorted Array: ",sortedArray)
           if(comparisonArray.join(',')===sortedArray.join(',')){
                  arrayOfProducts.push((a*b))
           };
           arrayOfNumbers=[];
           elementsOfAString=[];
           arrayOfNumbersFrom1toN=[];
           sortedArray=[]
          
                };
};
 uniq = [...new Set(arrayOfProducts)];
 summatoryOfElementsOfAnArray=0;
for(let i=0;i<uniq.length;i++){
     summatoryOfElementsOfAnArray=summatoryOfElementsOfAnArray+uniq[i];

}

             }
             return summatoryOfElementsOfAnArray;

}

console.log(pandigitalProducts(9));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36

Challenge Information:

Project Euler Problems 1 to 100 - Problem 32: Pandigital products

In what way specifically is it “not working”?

I do see

Potential infinite loop detected on line 21. Tests may fail if this is not changed.

which usually means the code is not efficient enough.

1 Like

Let me rethink it again, please. Do you have any suggestion?

One big thing is to use as few loop iterations and arrays as possible.

1 Like

I will do it. Thanks for your suggestion!

This topic was automatically closed after 60 days. New replies are no longer allowed.