- 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
Numbers smaller than the current number JavaScript
We are required to write a JavaScript function that takes in an array of Numbers as the only argument.
The function should return an array of Numbers that contains the number of elements smaller than each corresponding element in the original array.
For example −
If the input array is −
const arr = [3, 5 4, 1, 2];
Then the output should be −
const output = [2, 4, 3, 0, 1];
Example
const arr = [3, 5, 4, 1, 2]; const smallerNumbersThanCurrent = (arr = []) => { const res=[]; for(let i = 0; i < arr.length; i++){ let count = 0; let j = 0; while(j < arr.length){ if(arr[i] > arr[j]){ count++; j++; }else{ j++; }; }; res.push(count); }; return res; }; console.log(smallerNumbersThanCurrent(arr));
Output
And the output in the console will be −
[2, 4, 3, 0, 1]
- Related Articles
- Counting How Many Numbers Are Smaller Than the Current Number in JavaScript
- Counting smaller numbers after corresponding numbers in JavaScript
- C++ Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
- Write five rational numbers which are smaller than 2.
- The difference of squares of two numbers is 88. If the larger number is 5 less than twice the smaller number, then find the two numbers.
- Largest rectangle sum smaller than num in JavaScript
- Count of Binary Digit numbers smaller than N in C++
- An interesting solution to get all prime numbers smaller than n?
- Find Number of Array Elements Smaller than a Given Number in Java
- Number of smaller and larger elements - JavaScript
- Getting equal or greater than number from the list of numbers in JavaScript
- Just smaller number with monotone digits in JavaScript
- Checking if all array values are smaller than a limit using JavaScript
- The difference between the 2 natural numbers is 28. The smaller number is $frac{3}{4}$ of the greater number. Find the numbers.
- Print all Jumping Numbers smaller than or equal to a given value in C++

Advertisements