Where Do I Belong - function bug

Console gives a type error on the line where I try to push and sort everything. I would guess that my syntax is wrong:


function getIndexToIns(arr, num) {
  // Pseudo: compare the number against the array after sorting it. find the index of the number
  
  //Pseudo2: Slice arr[0] into arr_w_everything. Assign var marker to the argument to be tracked. Push all values into new_arr and sort ascending. Get indexOf marker.
  
  var arr_w_everything = Array.prototype.slice.call(arguments, 1);
  
  var marker = arr_w_everything[1];
  
  var new_arr = [];
  
  for (i = 0; i < arr_w_everything.length; i++) { 
  new_arr.push(arr_w_everything[i]).sort(function (a, b) {
    return a - b;
  });
  }
  
  return new_arr.indexOf(marker);
}

getIndexToIns([40, 60], 50);

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

1 Like

.push doesn’t return an array, so you get that error