1

In my JavaScript applet, I have to check the a and b input values: if they are a new pair, this pair is added to the history of the applet as a 1D vector.

So, I have made a History 2D array, where all the new pairs are stored. The program loops the array comparing the new pair with each row of the history array. However, I have a trouble: when the result of the comparison should be true (i.e., when i == 1), I get a false result.

I read this basic page: https://www.w3schools.com/js/js_arrays.asp And other several webpages, without to find the reason for my trouble.

This thread seems to talk about another issue (however, related to my question): Convert a 2D JavaScript array to a 1D array

I had tried to comparate with == and === operators, both cases without success.

I guess my mistake is some small bug, but I have several hours trying solving it without success (I am newbie with Javascript.)

var history = [[1, 1], [1, 2]];
var numA = 1;
var numB = 2;
var currentPair = [numA, numB];
var isRepeatedPair = false;
var i = 0;

while ((i < history.length) && (isRepeatedPair == false)) {
    isRepeatedPair = (currentPair === history[i]);
    // alert("i= " + i + "\n" + "Current pair: " + currentPair + "\n" + "Pair in history: " + history[i]+ "\n" + "Is repeated pair? " + isRepeatedPair);
    i = i + 1;
}

history.push(currentPair);

When the result of the comparison should be true (i.e., when i == 1), I get a false result.

(I have checked this with the command:

alert("i= " + i + "\n" + "Current pair: " + currentPair + "\n" + "Pair in history: " + history[i]+ "\n" + "Is repeated pair? " + isRepeatedPair);

in each step of the loop; this line is disabled in the MRE.)

  • Objects, like Arrays, can be identical without being equal check `[1,2,3] === [1,2,3]` in the console. Both `==` and `===` compare identity when you compare Objects. – Thomas Nov 11 '19 at 09:14
  • Possible duplicate of https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript – Andrew Nov 11 '19 at 09:16
  • Sidenote, you can write this check as `if (!history.some(([a, b]) => a === numA && b === numB)) history.push([numA, numB]);` in modern JS – Thomas Nov 11 '19 at 09:39

1 Answers1

1

You could take a diffrerent name of history, because this is a reserved variable of window.history.

Then change the loop and check only the length and exit the loop if a duplicate is found and by setting the flag isRepeatedPair.

The check has to compare every element, becaus an equal array is literally the same if it shares the same object reference. If you have only values, you need to check the values.

At the end check the flag again and if false, push the actual pair to the history.

var historyX = [[1, 1], [1, 2]],
    numA = 1,
    numB = 2,
    currentPair = [numA, numB],
    isRepeatedPair = false,
    i = 0;

while (i < historyX.length) {
    if (currentPair[0] === historyX[i][0] && currentPair[1] === historyX[i][1]) {
        isRepeatedPair = true;
        break;
    }
    i++;
}

if (!isRepeatedPair) {
    historyX.push(currentPair);
}
  
console.log(historyX);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392