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

86
Views
Array Weird Results in Cartesian product javasccript

I am stuck in the following code, i am getting all possible combination of layers from Photoshop. here in this example i have a group with three layers and (blue,purple,gold). I am taking these layers as array and the calling this combine function.

The problem is that every time a new combination is pushed in ""allCombination" array, the all combination array's previous element also turned to new element. In short if i had pushed [blue,purple] at allCombination[0] but when a push the second array combination as [blue,gold] in allCombination[1] The whole array "allCombination" become [blue,gold],[blue,gold]

var allCombinations = [];

var result = [];
result.length = 2; //n=2

function combine(input, len, start) {
  if(len === 0) {
    allCombinations.push(result); //process here the result
    $.writeln (allCombinations)
    return;
  }
  for (var i = start; i <= input.length - len; i++) {
    result[result.length - len] = input[i];
    combine(input, len-1, i+1 );
  }
}

var array = ["Blue", "purple", "gold"];    
  
combine( array, result.length, 0);

alert(allCombinations);

/////////////////////////////////

The above code gives following result

Blue,purple
Blue,gold,Blue,gold
purple,gold,purple,gold,purple,gold
about 4 years ago · Santiago Trujillo
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!