Head broke.
First in the for loop: for (var j = 0; j <= 7; j++) I push current_position.push(j); As a result, approximately the following array is generated:
var current_position = []; var cursor = 0; function queens8() { for (var j = 0; j <= 7; j++) { if (check(cursor, j) == true) { cursor++; current_position.push(j); if (current_position.length == 8) { done_positions.push(current_position); console.log(current_position); //выводит на первой итерации: [0, 4, 7, 5, 2, 6, 1, 3] } queens8(); } } current_position.splice(cursor-1, 1); cursor--; return; } But if you collect all the arrays in another like this:
done_positions.push(current_position); I get something like this:
[Array[0], Array[0], Array[0], Array[0], Array[0], Array[0], ...] And so all 120 passes. Why are they empty and how to make them not empty?
UPDATE: I fixed the problem, but I don’t understand why it worked:
done_positions.push([]+current_position);
done_positions.push([]+current_position);What!? Why is that? - Telionjgenerated by theforloop. And already the conditions of selection and bringingjinto the array are not necessary here and will only clog the purity of the code. I thought so, and so I cut the code as much as possible. - Telioncurrent_position, where is it created, how can it be[0, 4, 7, 5, 2, 6, 1, 3]? "My bewilderment was shared by all Europe" V.Erofeyev - Igor