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)