"Where do I belong" - need help understanding answer better

"Where do I belong" - need help understanding answer better
0.0 0

#1

I just completed the Where do I belong problem, but I don’t necessarily understand a certain aspect of my code.


function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  var index;
  var sortedArr = arr.unshift(60); //this line I don't understand 
  
  sortedArr = arr.sort(function(a,b){
    return a-b;
  });

    for (var i=0 ; i< sortedArr.length ; i++){
    if (sortedArr[i] >= num ){
      index = sortedArr.indexOf(sortedArr[i]);    
      break;//or return index 
    }   
  }
  
   return   index;
  
  //check for the first number that is  bigger, return index
}

getIndexToIns([2, 5, 10], 15);

I am pretty sure the only reason my arr.unshift() method works is because it satisfies strictly the points FCC wants to you to complete before moving on - but it would not work for a num variable that is larger than the value I am unshifting to the array. For example, the highest num argument FCC gives you on this challenge is 60, so if you unshift any number lower than 60, you will no satisfy all of the conditions. However, if one of the num augments FCC gives you was greater than 60, lets say 100, than this solution would not work, and I would have to change to change the unshift method to insert a number of 100 or higher. Correct?

Can someone confirm this/help me out?

I understand everything else. Because I added the unshift method, all of the conditions were satisfied besides the last one, because an index for the num we wanted to pass in did not exist, which is why I chose to unshift an element into the array, to create an index for the last value in getIndexToIns([2, 5, 10], 15)


#2

The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.

In this challenge you only need to return the location that the new number would go; you don’t have to change the array at all. Let’s talk it out:

let arr = [1, 3, 5 ,7] and we want to know where var num = 6 goes. We could do something like:

If num is less than array[0] then return 0
else if num is less than array[1] then return 1

and so on. No manipulation of the original array is required.