0

I want to check a value is contained in the set 3,8,13,15,18,23,28, ..., etc.; basically the set is defined by 3 + 5n.

How can I achieve this with a simple condition?

Code:

if(/* condition here */) {
  // value is part of set
} else {
  // value is not part of set
}
musefan
  • 47,875
  • 21
  • 135
  • 185
user1386654
  • 51
  • 3
  • 9

2 Answers2

6

if you have to check if your variable is in those values which is (3 + 5n), you should use this:

var val = 8; //for example
if ((val % 5) == 3) {
//do the code
} 
else {
//do other code
}
Gintas K
  • 1,438
  • 3
  • 18
  • 38
-3

jquery has a inarray function. you can use it as follows.

var val = $("#item").val();
var arr = [3,8,13,15,18,23,28];

if ($.inArray(val, arr)) {
    //do the code
} else {
   //do other code
}
DevZer0
  • 13,433
  • 7
  • 27
  • 51
  • 1
    Where's the jquery tag? – Ja͢ck Jul 11 '13 at 10:58
  • 1
    @Jack it is removed now – Arun P Johny Jul 11 '13 at 10:59
  • the values i want to check for are not just till 28, but all values that can come after adding 5 to current value, for e.g 28,33,38,43,48 and so on – user1386654 Jul 11 '13 at 11:00
  • Or you can use just arr.indexOf(val)>-1 – Pablo Lozano Jul 11 '13 at 11:01
  • 1
    @PabloLozano Doesn't work in IE < 9. – Ja͢ck Jul 11 '13 at 11:02
  • 2
    this should not in anyway be the accepted answer, this so much lamer than [3,8,13,15,18,23,28].indexOf(num) > -1 – NimChimpsky Jul 11 '13 at 11:11
  • This is suspicious... Why would you get the value from an html element without any mention of it from the OP? Why would you use JQuery? Are you cheating the rep system? Are you the OP? – musefan Jul 11 '13 at 11:14
  • @musefan OP has been a member a lot longer than this guy so I suspect not, it's the OP's fault for being unclear on the requirements I'm guessing. – dsgriffin Jul 11 '13 at 11:17
  • the question title says how to do with jquery its not my fault the question title has changed since @musefan – DevZer0 Jul 11 '13 at 11:42
  • @musefan your the one who changed the title and removed jquery and what type of drama you trying to start. – DevZer0 Jul 11 '13 at 11:44
  • @Zenith check the edit changes of the question musefan was the one who removed jquery – DevZer0 Jul 11 '13 at 11:49
  • @DevZer0: I changed the title based on what the OP said he wanted in the comments. And regardless of how you decide to answer. There is absolutely no need for JQuery and the question itself contained no use of JQuery – musefan Jul 11 '13 at 12:11
  • i didn't see that thread of communication, i was working under the original question. I am sorry it hurt your feelings i didn't try to get on your territory @musefan – DevZer0 Jul 11 '13 at 12:15