I'm currently tackling a JavaScript challenge that requires writing a function to determine the lowest index at which a value (second argument) should be inserted into a sorted array (first argument). For instance, if we call where([1,2,3,4], 1.5)
, the expected output is 1
because 1.5 falls between 1 (0th index)
and 2 (1st index)
.
The provided hint suggests utilizing the built-in ".sort()
" method, with which I am somewhat unfamiliar prior to this challenge. Below is my attempt so far, although I suspect it's quite off-track.
function where(arr, num) {
arr.push(num).sort(function(a,b){return a-b;});
return arr.indexOf(num);
}
console.log(where([40, 60], 50)); // outputs "unexpected identifier"