Created
October 17, 2021 18:35
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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