Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How many numbers in the given array are less/equal to the given value using the percentile formula in Javascript?
In this article, you will understand how to calculate the percentile of a given value in an array using JavaScript. The percentile tells us what percentage of numbers in the array are less than or equal to a specific value.
Percentile Formula
We use the following formula to calculate the percentile:
Percentile = (n/N) * 100
Where:
- n = count of values less than or equal to the given value
- N = total number of values in the array
For values equal to our target, we typically count them as 0.5 to get a more accurate percentile calculation.
Method 1: Using for...in Loop
This approach uses a for-loop to iterate through the array and count values less than or equal to the target value.
const calculatePercentile = (inputArray, val) => {
let count = 0;
for (let i in inputArray) {
count += (inputArray[i] < val ? 1 : 0) + (inputArray[i] === val ? 0.5 : 0);
}
let percentile = (count / inputArray.length) * 100;
console.log("The percentile value is:", percentile);
return percentile;
};
const inputArray = [3, 4, 5, 6, 7, 8];
console.log("The array is:", inputArray);
console.log("Target value: 6");
calculatePercentile(inputArray, 6);
The array is: [ 3, 4, 5, 6, 7, 8 ] Target value: 6 The percentile value is: 58.333333333333336
Method 2: Using reduce() Method
This approach uses the reduce() method to count values and calculate the percentile in a more functional programming style.
const calculatePercentile = (inputArray, inputValue) =>
(100 *
inputArray.reduce(
(count, value) => count + (value < inputValue ? 1 : 0) + (value === inputValue ? 0.5 : 0),
0
)) / inputArray.length;
const inputArray = [3, 4, 5, 6, 7, 8];
console.log("The array is:", inputArray);
let inputValue = 6;
console.log("Target value:", inputValue);
console.log("The percentile value is:", calculatePercentile(inputArray, inputValue));
The array is: [ 3, 4, 5, 6, 7, 8 ] Target value: 6 The percentile value is: 58.333333333333336
How It Works
Both methods follow the same logic:
- Count values less than the target (full count = 1)
- Count values equal to the target (half count = 0.5)
- Apply the percentile formula: (count / total) × 100
In our example with array [3, 4, 5, 6, 7, 8] and target value 6:
- Values less than 6: [3, 4, 5] = 3 values
- Values equal to 6: [6] = 1 value × 0.5 = 0.5
- Total count: 3 + 0.5 = 3.5
- Percentile: (3.5 / 6) × 100 = 58.33%
Comparison
| Method | Readability | Performance | Style |
|---|---|---|---|
| for...in Loop | More explicit | Slightly faster | Imperative |
| reduce() | More concise | Slightly slower | Functional |
Conclusion
Both methods effectively calculate percentiles using the standard formula. The for-loop approach is more explicit and readable, while the reduce() method offers a more functional programming style. Choose based on your coding preferences and team standards.
