Ryan
Ryan

Reputation: 45

checking if input is greater or lesser than array values

Sorry if this is a duplicate.

Let's say I have an array of 12 numbers, and I want to check those values against an input and then output how many values inside the array are higher than the input and lower than the input.

I wasn't sure how to do this in jQuery but if it's not possible in jQuery I am open to other suggestions. I wasn't sure if using the inArray or grep function would even work for this type of implementation.

Upvotes: 0

Views: 56

Answers (1)

AR_HZ
AR_HZ

Reputation: 365

Using jQuery you can run a foreach loop and count how many values are higher and lower from the array you have.

Try this:

var higher = 0;
var lower = 0;
$.each(arr, function(k,v){
// will miss if value is equals.
if (input < v) higher ++;
if (input > v) lower ++; 
})
console.log("Higher values are: "+higher+ " | Lower values are: "+lower);

Assuming arr is your array and input variable is what user inputs.

ref: http://api.jquery.com/jquery.each/

Upvotes: 2

Related Questions