You can use the .index(), You don’t need jQuery.
If you are going to repeat this check several times you can do a function for this.
I suggest to store these numbers in arrays:
var numerosA = [1,3,5,6,7,9,10];
var numerosB = [2,4,8,11,12,17];
and then check the new number with these:
numerosA.indexOf(1) != -1 // true
numerosA.indexOf(2) != -1 // false
numerosB.indexOf(1) != -1 // false
numerosB.indexOf(2) != -1 // true
The .index() gives the position of that number within the array. If the number does not exist in the array, it will give -1
.
You can also use a ternary for this. Then you can do:
var resultado = numerosA.indexOf(seuNumero) != -1 ? 'Sim' : 'Não';
Example applied to a function, in this example giving a third answer to erroneous cases:
var numerosA = [1, 3, 5, 6, 7, 9, 10];
var numerosB = [2, 4, 8, 11, 12, 17];
function verificar(nr) {
if (numerosA.indexOf(nr) != -1) return 'Sim!';
else if (numerosB.indexOf(nr) != -1) return 'Não!';
return 'Não existe em nenhuma...';
}
alert(verificar(1)); // Sim!
alert(verificar(2)); // Não!
alert(verificar(500)); // Não existe em nenhuma...
Are these exactly the numbers that will show "Yes" and "No" or do they have any meaning behind the numbers? Also, why use jQuery to compare numbers? It couldn’t be done in pure javascript?
– Thomas