Skip to content

Instantly share code, notes, and snippets.

@lior-amsalem
Created October 17, 2021 18:35
Show Gist options
  • Save lior-amsalem/aa9a409d071ec9c30aec68f58589eeb8 to your computer and use it in GitHub Desktop.
Save lior-amsalem/aa9a409d071ec9c30aec68f58589eeb8 to your computer and use it in GitHub Desktop.
given array of numbers, return array of numbers that tells how many numbers to the right are smaller than current value
function smaller(nums) {
/** developer comment:
- we have to return the amount of numbers that are smaller than arr[i] to the right.
- example: smaller([5, 4, 3, 2, 1]) === [4, 3, 2, 1, 0] or smaller([1, 2, 0]) === [1, 1, 0]
- go over each elemt
- cut with slice base on current index
- filter which one is smaller then current value
- use lenght to count how smaller than me?
**/
let res = nums.map((value, index, array) => {
return array.slice(index).filter(currentValue => currentValue < value).length;
});
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment