O
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 the 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 are not the same size, I already consider that they are not the same.Following the same previous logic, we can use the method every of the 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 choose 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 the data independently of the 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 worth mentioning that the method sort will change the original array if you do not want this to happen, you can use the method slice before sort, thus creating a copy of the array, so the array sent to the function will not suffer 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);Documents: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/toString https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/every https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/sort https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/slice https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Statements/for https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/length