Need help to fix a snippet from my math grid maze solver

42 views Asked by At

The idea behind the following code is to test if any number between 0 and 13 + any other number equals 13. If one does both numbers should be saved to a different array but on the same index. So i should have all possible combinations to reach 13 in 2 arrays. But when i run my code I only get 2 combinations which are 0+13 and 13+0. Here is the code:

var number1 = [];
var number2 = [];
var index = 0;
var i = 0;
var j = 0;

//Tests if i + j (from the loop) add up to 13
var test = function(i, j) {
  if (i + j === 13) {
    number1[index] = i;
    number2[index] = j;
    index =+ 1;
  }
}

//1st loop generates i from 0 to 13 in 0.5 step.
for (i = 0; i < 13.5; i += 0.5) {

  //same for j, this number should test with i every loop
  for (j = 0; j < 13.5; j += 0.5) {
    test(i, j);
  }
}

//outputs the 2 arrays, the matching numbers should be stored in
for (i = 0; i < number1.length; i++) {
  console.log(number1[i]);
  console.log(number2[i]);
}

1

There are 1 answers

1
Gabriele Petrioli On BEST ANSWER

Change index =+ 1 to index += 1

Then index =+ 1 sets the index to 1 it does not increment it by 1 (as you want)


See Expressions and operators: Assignment operators MDN