This challenge requires returning the lowest index at which a value that is second argument should be
inserted into an array which is the first argument after sorting it.
The algorithm was quite straight forward, only thing that made me to think was what if the number is to inserted at index greater than last index of the array e.
Clue is given in the form of link below:
This JavaScript method sort() is quite self explanatory. It sorts the array so that we can find the right index of the second argument.
So here is my implementation of the algorithm.
The algorithm was quite straight forward, only thing that made me to think was what if the number is to inserted at index greater than last index of the array e.
Clue is given in the form of link below:
This JavaScript method sort() is quite self explanatory. It sorts the array so that we can find the right index of the second argument.
So here is my implementation of the algorithm.
function where(arr, num) {
arr1 = arr.sort(function(a, b) { //sort the array first
return a - b;
});
for (var i = 0; i <= arr1.length; i++){
if (num <= arr1[i]){
return i;
}
if (arr[i] === undefined){
return i;
}
}
}
No comments:
Post a Comment