i need to iterate an array of x numbers in given order and calculate all possible combinations, for example:
I have this Array of 5 numbers [01,02,03,04,05], this is the condition to calculate the combinations:
expected output:
01 02 01 03 01 04 01 05 05 04 05 03 05 02 04 03 04 02 03 02
Example of array of 6 numbers [00,00,00,10,40,60], this is the condition to calculate the combinations:
expected output:
00 00 00 00 00 10 00 40 00 60 60 40 60 10 60 00 60 00 00 00 00 10 00 40 40 10 40 00 00 10
so the array can be from length 3 to x length.
this is what i have
function calculate(numbers){
let combinations = [];
if(numbers.length == 2){
combinations = [numbers[0] + ' '+ numbers[1]];
}else{
for(let i = 1; i < numbers.length; i++){
combinations.push(numbers[0] + ' '+ numbers[i]);
}
for(let i = numbers.length - 1; i >= 1; i--){
combinations.push(numbers[numbers.length - 1] + ' '+ numbers[i]);
}
}
return combinations
}
let numbers = [01,02,03,04,05];
console.log(calculate(numbers))
thanks in advance