Basic Algorithm Scripting: Where do I Belong

Basic Algorithm Scripting: Where do I Belong
0.0 0


Tell us what’s happening:
could not pass this one
getIndexToIns([5, 3, 20, 3], 5) should return 2

Your code so far

function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  let i =0;
  let newArr = arr.sort();
  for (i=0; i<newArr.length; i++)
  if (newArr[i]>=num)
  return i;

return newArr.length;

getIndexToIns([40, 60], 50);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:


.sort treats array elements as strings by default, so it often doesn’t result in an array that’s numerically sorted.

Pass it this function to sort the array numerically:

arr.sort(function(a, b) {
  return a - b;