Compare a variable with every member of an array? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Compare a variable with every member of an array?

So if I want to compare a varibale with an array if(var = arr[0] || var = arr[1] || var = arr[2] ...) is there a possibility to do it easier, faster, with less writing? something like if(var = arr[all]) or for(int i = 0; i < arr.length; i++) if(var == arr[i]) //do stuff ?

18th May 2017, 6:56 PM
Vodolle
Vodolle - avatar
2 Answers
+ 10
for (var i=0; i< arr.length; i++) { if (var == arr[i]) { // Code here } }
18th May 2017, 6:58 PM
Gami
Gami - avatar
+ 1
I figured it out by now seems like the one in my example .. maybe I shouldn't hesitate and ask everything before experementing thank you for your contribution
18th May 2017, 8:43 PM
Vodolle
Vodolle - avatar