Skip to content

Latest commit

 

History

History
30 lines (23 loc) · 544 Bytes

File metadata and controls

30 lines (23 loc) · 544 Bytes

Exercise

How many are smaller than me?

Write

function smaller(arr)

that given an array arr, you have to return the amount of numbers that are smaller than arr[i] to the right.

For example:

smaller([5, 4, 3, 2, 1]) === [4, 3, 2, 1, 0]
smaller([1, 2, 0]) === [1, 1, 0]

Solution

function smaller(arr) {
  return arr.reduce((prev, curr, index, arr) => {
    const arrRest = arr.slice(index);
    const times = arrRest.filter(val => val < curr).length;
    prev[index] = times;
    return prev;
  },[])
}