- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Counting below / par elements from an array - JavaScript
We are required to write a function that counts how many of the elements are in the array below / above a given number.
Following is our array of Numbers −
const array = [54,54,65,73,43,78,54,54,76,3,23,78];
For example, if the number is 60, the answer should be five elements below it −
54,54,43,3,23
and five element par it −
65,73,78,76,78
Example
Following is the code −
const array = [54,54,65,73,43,78,54,54,76,3,23,78]; const belowParNumbers = (arr, num) => { return arr.reduce((acc, val) => { const legend = ['par', 'below']; const isBelow = val < num; acc[legend[+isBelow]]++; return acc; }, { below: 0, par: 0 }); }; console.log(belowParNumbers(array, 50)); console.log(belowParNumbers(array, 60)); console.log(belowParNumbers(array, 70));
Output
This will produce the following output in console −
{ below: 3, par: 9 } { below: 7, par: 5 } { below: 8, par: 4 }
- Related Articles
- Counting unique elements in an array in JavaScript
- Counting possible APs within an array in JavaScript
- Counting elements of an array using a recursive function in JS?
- Counting the occurrences of JavaScript array elements and put in a new 2d array
- JavaScript - How to pick random elements from an array?
- Counting frequencies of array elements in C++
- Counting number of triangle sides in an array in JavaScript
- How to filter an array from all elements of another array – JavaScript?
- JavaScript construct an array with elements repeating from a string
- How to remove duplicate elements from an array in JavaScript?
- How to count the number of elements in an array below/above a given number (JavaScript)
- Grouping an Array and Counting items creating new array based on Groups in JavaScript
- How to remove certain number elements from an array in JavaScript
- Remove elements from array using JavaScript filter - JavaScript
- Return Top two elements from array JavaScript

Advertisements