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.)