lundi 31 juillet 2017

Reference any element in array

I made a script that makes 3 array elements, each of which equal:

Number(prompt())

Then I make an if statement saying:

if (!arr[0] || !arr[1] || !arr[2]) {alert("invalid data")}

This is ok for a couple of elements, however if I want to add say 10 elements then adding a bunch of logical OR operators is sort of clunky.

So I'm wondering if there is a way to just tell the interpreter "if any element in the array is not a number then alert an error else do alert(a)". (If that makes sense?.)

I am aware of the 'in' operator/keyword but this seems to check an array element name against the array name to see if it exists in that array. This is not quite what I'm after however.

I've also seen that there is a some() property/function but am not that far into learning JS yet.

Full script fiddle: http://ift.tt/2uc4Vkz

Thanks.

Aucun commentaire:

Enregistrer un commentaire