There are several ways to compare data from an array.
Whereas you have a simple array, being only a vector without complex objects, we can for example create a function and make a for, comparing data based on the array index:
function comparaArrays(sorteados, nao_Sorteados) {
if (sorteados.length != nao_Sorteados.length) {
return false;
}
for (let i = 0; i < nao_Sorteados.length; i++) {
if (sorteados[i] !== nao_Sorteados[i]) {
return false;
}
}
return true;
}
const array1 = [1, 2, 3, 4, 5, 6];
const array2 = [1, 2, 3, 4, 5, 6];
if (comparaArrays(array1, array2)) {
console.log("Parabéns, você acertou tudo");
} else {
console.log("Infelizmente você errou");
}
const array3 = [1, 2, 3, 4, 5];
const array4 = [1, 2, 3, 4, 6];
if (comparaArrays(array3, array4)) {
console.log("Parabéns, você acertou tudo");
} else {
console.log("Infelizmente você errou");
}
Note that if the arrays do not have the same size, I already consider that they are not equal.
Following the same previous logic, we can use the method every array, greatly shortening our code:
function comparaArrays(sorteados, nao_Sorteados) {
if (sorteados.length != nao_Sorteados.length) {
return false;
}
return nao_Sorteados.every( (value, index) => value === sorteados[index] );
}
const array1 = [1, 2, 3, 4, 5, 6];
const array2 = [1, 2, 3, 4, 5, 6];
if (comparaArrays(array1, array2)) {
console.log("Parabéns, você acertou tudo");
} else {
console.log("Infelizmente você errou");
}
const array3 = [1, 2, 3, 4, 5];
const array4 = [1, 2, 3, 4, 6];
if (comparaArrays(array3, array4)) {
console.log("Parabéns, você acertou tudo");
} else {
console.log("Infelizmente você errou");
}
We can also opt for a simple option by converting the array to string and comparing the results:
function comparaArrays(sorteados, nao_Sorteados) {
return nao_Sorteados.toString() === sorteados.toString();
}
const array1 = [1, 2, 3, 4, 5, 6];
const array2 = [1, 2, 3, 4, 5, 6];
if (comparaArrays(array1, array2)) {
console.log("Parabéns, você acertou tudo");
} else {
console.log("Infelizmente você errou");
}
const array3 = [1, 2, 3, 4, 5];
const array4 = [1, 2, 3, 4, 6];
if (comparaArrays(array3, array4)) {
console.log("Parabéns, você acertou tudo");
} else {
console.log("Infelizmente você errou");
}
Obs: If you want to compare data independent of ordering, you could call the method sort before comparing the data:
function comparaArrays(sorteados, nao_Sorteados) {
if (sorteados.length != nao_Sorteados.length) {
return false;
}
sorteados.sort();
return nao_Sorteados.sort().every( (value, index) => value === sorteados[index] );
}
const array1 = [1, 2, 3, 4, 5, 6];
const array2 = [6, 5, 4, 3, 2, 1];
if (comparaArrays(array1, array2)) {
console.log("Parabéns, você acertou tudo");
} else {
console.log("Infelizmente você errou");
}
It is valid to mention that the method sort will change the original array, if you do not want this to happen, you can use the method slice before the sort, thus creating a copy of the array, so the array sent to the function will not undergo any change:
function comparaArrays(sorteados, nao_Sorteados) {
if (sorteados.length != nao_Sorteados.length) {
return false;
}
const paraComparar = sorteados.slice().sort();
return nao_Sorteados.slice().sort().every( (value, index) => value === paraComparar[index] );
}
const array1 = [1, 2, 3, 4, 5, 6];
const array2 = [6, 5, 4, 3, 2, 1];
console.log("Array antes: ", array2);
if (comparaArrays(array1, array2)) {
console.log("Parabéns, você acertou tudo");
} else {
console.log("Infelizmente você errou");
}
console.log("Array depois: ", array2);
Documentations:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length
Imagine two arrays:
[0, 2, 1]and[0, 1, 2]. Should the result of the comparison be positive or negative? That is, do you seek to be identical even in the position of the elements? (and another question: you only have 1 depth level or each element of the array can have sub-arrays?)– Sergio