I had to solve the following problem:
9 values, from 1 to 9 (0 and 10 not accepted) and all numbers need to be different.
To solve the probem, I made those horrible for loops inside for loops.
(I added 2 more conditions to check if i had one of the solutions)
It is working, but I was wondering how to create those for loops inside for loops in a better way?
Also, each number can't be equal to another. How can you accomplish this another way than I did? (Again, 2 first conditions can be deleted)
Here is code:
var a = 1, b = 1, c = 1, d = 1, e = 1, f = 1, g = 1, h = 1, i = 1;
var x = 0;
var result = [];
function calc(){
x = a + 13 * b / c + d + 12 * e - f - 11 + g * h / i - 10;
if(x == 66){
result.push([a, b , c , d , e, f, g, h, i] );
}
}
for(a = 1; a < 10; a++){
calc();
for(b = 1; b < 10; b++){
calc();
for(c = 1; c < 10; c++){
calc();
for(d = 1; d < 10; d++){
calc();
for(e = 1; e < 10; e++){
calc();
for(f = 1; f < 10; f++){
calc();
for(g = 1; g < 10; g++){
calc();
for(h = 1; h < 10; h++){
calc();
for(i = 1; i < 10; i++){
calc();
}
}
}
}
}
}
}
}
}
console.log(result);
var result2 = result.filter(function(el){
return el[0] == 5 && el[1] == 9 && el[0] != el[1] && el[0] != el[2] && el[0] != el[3] && el[0] != el[4] && el[0] != el[5] && el[0] != el[6] && el[0] != el[7] && el[0] != el[8] && el[1] != el[0] && el[1] != el[2] && el[1] != el[3] && el[1] != el[4] && el[1] != el[5] && el[1] != el[6] && el[1] != el[7] && el[1] != el[8] && el[2] != el[0] && el[2] != el[1] && el[2] != el[3] && el[2] != el[4] && el[2] != el[5] && el[2] != el[6] && el[2] != el[7] && el[2] != el[8] && el[3] != el[0] && el[3] != el[1] && el[3] != el[2] && el[3] != el[4] && el[3] != el[5] && el[3] != el[6] && el[3] != el[7] && el[3] != el[8] && el[4] != el[0] && el[4] != el[1] && el[4] != el[2] && el[4] != el[3] && el[4] != el[5] && el[4] != el[6] && el[4] != el[7] && el[4] != el[8] && el[5] != el[0] && el[5] != el[1] && el[5] != el[2] && el[5] != el[3] && el[5] != el[4] && el[5] != el[6] && el[5] != el[7] && el[5] != el[8] && el[6] != el[1] && el[6] != el[2] && el[6] != el[3] && el[6] != el[4] && el[6] != el[5] && el[6] != el[7] && el[6] != el[8] && el[7] != el[0] && el[7] != el[1] && [7] != el[2] && el[7] != el[3] && el[7] != el[4] && el[7] != el[5] && el[7] != el[6] && el[7] != el[8] && el[8] != el[0] && el[8] != el[1] && el[8] != el[2] && el[8] != el[3] && el[8] != el[4] && el[8] != el[5] && el[8] != el[6] && el[8] != el[7];
});
console.log(result2);