# How many numbers in the given array are less/equal to the given value using the percentile formula ?

The following approach covers how to calculate how many numbers in the given array are less or equal to the given value using the percentile formula in JavaScript.

Problem Statement: You are given an array containing different integer values and also an integer value. You need to see how many numbers in the given array are less or equal to the given integer value and return the percentile value of the result using the percentile formula in JavaScript.

As an example take the array which is [1,2,3,4,5,6] and the given integer value is 6. So if we count how many elements are there which are less than and equal to 6 then we will see that there are 6 elements (1,2,3,4,5,6) that are less than and equal to 6.

Therefore, according to the percentage formula, we will print our result as 100 percent since all the elements are less than and equal to the given integer value. There are several approaches to solving this particular problem. We have covered the following two approaches:

Approach 1:

• In this approach, we will use for-in loop for checking particularly each element of an array.
• Inside the for loop, we will keep our logic part and after finishing the logic part we will print our result in the console by calling a function (or a method) in which we are writing down our code.
• We have used the ternary operator here inside the for-in loop instead of using an if-else statement which will make our piece of code a little less lengthy and easily understandable.

Example: In this example, we will be using the Javascript for-in loop to check how many numbers are less than or equal to a given number.

## Javascript

 ``

Output:

`75`

Approach 2:

• In this approach, we will use reduce() method which will traverse our array and convert it into a single entity or a single value.
• While calling the reduce() method we will write our logic part inside reduce() method and will call our main method or a function for printing the resultant value.
• In the reduce() method, we will take two arguments: the first one is the “acc” variable which is termed an accumulator which will act as our result value in which we could store our result after calculation, and the second is “v” which is called as value variable in which each value of an array is stored and by using this “v” variable we will compare each value with the value given to us for comparison.

Example: In this example, we will be using the Javascript reduce() method to check how many numbers are less than or equal to a given number.

## Javascript

 ``

Output:

`75`

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next