I have to validate that the values of a two-dimensional array are consecutive. I did two nested loops to compare each element to a counter, but when I run the function it returns false whatever the case may be and I can't see why.
For example, the following array should return me true
:
x = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
And the following should return me false
:
x = [
[2, 1, 3],
[4, 5, 6],
[7, 8, 9]
];
The code I have so far:
var x = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
function chequear() {
var contador = 0;
for (i = 0; i < x.length; i++) {
for (j = 0; j < x.length; j++){
if (x[i][j] == contador++) {
return true
} else {
return false
}
}
}
}
chequear();
One of the errors is that in both
for
you are usingx.length
the number of iterations as a limit. In the second you should use something likex[0].length
to iterate through the number of elements of the inner array.When using
x.length
you get the number of rows. Strictly it is the number of elements of the array of the first level/dimension or exterior.Using something like
x[0].length
you get the number of columns. Strictly it is the number of elements of the first array that is a member of the first level array or the second or inner dimension.When dealing with a square matrix (same number of rows and columns), as in this case, you can use the given example, but if it were a " jagged array " (the elements of the inner arrays are not the same size , or it is not certain that they are) you should use
x[i].length
. Strictly speaking, a two-dimensional array is a type of "jagged array".Here is the code from the question with a few minor modifications, 1) the change suggested above, 2) the counter counts the number of elements in the two-dimensional array
The other problem with the code is the logic of what you want to do with each element of the array, but we won't be able to help you with that until you describe it clearly.
There are two things to keep in mind with your code:
1 - Once you do a return inside the loops, you will no longer check the following values. Inside the second cycle you have a condition, if the first value of the array matches you
contador
returntrue
, if it doesn't match, you returnfalse
, but returningtrue
does not mean that you have checked if the entire array meets the condition, it just means that the first value matches your check. Here I place your same code with aconsole.log
before the check so that you notice how many times said is executedconsole.log
:2 - The second thing to keep in mind is that the operator
++
acts differently depending on where you place it with respect to the operand. If you place it after the operator, it will first return the value of this and then it will increment it by1
, if you place it before, it will first increment it by1
and then it will return the final incremented value. Look at the following snippet so you understand how each one works:You will then understand why it returns you
false
, the value of the variablecontador
is0
at the beginning, in your check, by having the operator++
afterwards, you will check the first time if it1
is equal to0
, the second time if it2
is equal to1
and so on consecutively. Your validation will never be true.Therefore, with these observations in mind, here is a variation of your code. By default I place a
return
with valuetrue
at the end of the function, if the cycles run completely and finish, it means that the values of the array pass the condition. But there is a condition in the second loop, using the operator++
before the operand, that checks if any value is different, and if it is, returnsfalse
. You can check the function in the following snippet using two arrays, the first one is valid and the second one is not: