I am trying to learn some AngularJS creating a "simple" math tool that calculates a distribution.

I have an array of numbers and I managed to get the list of unique values with number of occurrences and percentage in an object (the unique number as the key, occurrences, percentage and the progressive percentage as the values). Example:

```
$scope.values = {"17" : [1, 0.02, 0.02],"18" : [6, 0.12, 0.14],"19" : [7, 0.14, 0.28],"20" : [9, 0.18, 0.46],"21" : [13, 0.26, 0.72], "22" : [4, 0.08, 0.8],"23" : [5, 0.1, 0.9],"24" : [1, 0.02, 0.92],"25" : [3, 0.06, 0.98],"26" : [1, 0.02, 1]}

```

The unique number is the key while the 3 values are respectively the number of occurrences, the percentage on the overall iterations and the progressive percentage. Example, the number 25 occurred 3 times, i.e. 6% of the times, and includes 98% of the occurrences of numbers smaller or equal to 25.

**Problem**  

Given a "progressive percentage" I need to get the key of the nearest value to that percentage. So, the search must be performed only on the 3rd values (if that changes anything), should identify the closest value (select the greater of two equidistant values), should return the **key**.

In the example above:

* if I search for 0.75, I should get 21,
* if I search for 0.91, I should get 23.

Any help with this?

This is where I got so far:

```
function nearest(num, ar) {
        var i = 0,
            closest, closestDiff, currentDiff;
        for (i; i < ar.length; i++) {
            if (closest) {
                closestDiff = Math.abs(num - closest);
                currentDiff = Math.abs(num - ar[i]);
                if (currentDiff < closestDiff) {
                    closest = ar[i];
                }
                closestDiff = null;
                currentDiff = null;
            } else if (!closest) {
                //give initial value
                closest = ar[i];
            }
        }
function (pct) {
    $scope.milestones = [];
    var ms = $scope.getClosestNum(pct, $scope.values);
    $scope.milestones.push(ms);
    return $scope.milestones;
  };

```

But `{{getMilestones(0.75)}}` in my HTML doesn't return anything.