Chris
Chris

Reputation: 453

Return number less than or equal to with a limit of 5 using jquery

I have some jQuery which returns if the number is less than or equal to the selectedyear

return parseInt(number, 10) <= selectedyear;

I want to keep this functionality but limit the less than to only 5 deep.

e.g.1). If the number was 10 I would want all numbers less than 10 - but only the first 5 values. e.g 10,9,8,7,6

e.g.2). If the number was 22 I would want all numbers less than 22 - but only the first 5 values. e.g 22,21,20,19,18

Not sure if this is even possible - or there is an easier way around this.

Upvotes: 0

Views: 980

Answers (1)

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123397

You could define a function accepting both the number to check and the limit for the comparison.

If num is lower or equal to limit and num is greater than limit - 5 then return true

function isInRange(num, limit) {
   return (num <= limit) && (num > limit - 5);
}

if (isInRange(number, selectedYear)) {
   ...
}

Upvotes: 1

Related Questions