9.6 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	title
| title | 
|---|
| Where Do I Belong | 
 Remember to use
 Remember to use Read-Search-Ask if you get stuck. Try to pair program  and write your own code
 and write your own code 
 Problem Explanation:
 Problem Explanation:
This can be a tricky problem to understand. You need to find where in the array a number should be inserted by order, and return the index where it should go.
Relevant Links
 Hint: 1
 Hint: 1
The first thing to do is sort the array from lower to bigger, just to make the code easier. This is where sort comes in, it needs a callback function so you have to create it.
try to solve the problem now
 Hint: 2
 Hint: 2
Once the array is sorted, then just check for the first number that is bigger and return the index.
try to solve the problem now
 Hint: 3
 Hint: 3
If there is no index for that number then you will have to deal with that case too.
try to solve the problem now
Spoiler Alert!
Solution ahead!
 Basic Code Solution:
 Basic Code Solution:
function getIndexToIns(arr, num) {
  arr.sort(function(a, b) {
    return a - b;
  });
  for (var a = 0; a < arr.length; a++) {
    if (arr[a] >= num)
      return a;
  }
  return arr.length;
}
Code Explanation:
- First I sort the array using .sort(callbackFuntion)to sort it by lowest to highest, from left to right.
- Then I use a for loop to compare the items in the array starting from the smallest one. When an item on the array is greater than the number we are comparing against, then we return the index as an integer.
Relevant Links
 Basic Code Solution:
 Basic Code Solution:
function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  var times = arr.length; // runs the for loop once for each thing in the array
  var count = 0; 
  for (var i=0;i<times;i++){
    if(num>arr[i]){count++;} } // counts how many array numbers are smaller than num
    return count; // the above equals num's position in a sorted array
}
getIndexToIns([40, 60], 50);
Code Explanation:
- I do not sort the arr input array
- I run a for loop counting whenever the num input is bigger than an arr input number.
- This number is equivalent to what num's position would be in a sorted array.
 Basic Code Solution:
 Basic Code Solution:
by @HarinaPana
function getIndexToIns(arr, num) {
  arr.sort(function(a, b) {
  return a - b;
  });
  var i = 0;
  while (num > arr[i]) {
  i++;
  }
  return i;
}
getIndexToIns([40, 60], 50);
Code Explanation:
- Sort existing array.
- Iterate through the array while checking if num is bigger.
- The loop will stop when num isn't bigger than i and return the last element checked.
 Intermediate Code Solution:
 Intermediate Code Solution:
by @faustodc
function getIndexToIns(arr, num) {
  arr.push(num);
  arr.sort(function(a, b){return a-b});
  return arr.indexOf(num);
}
Code Explanation:
- First we add the number numto the array usingpush()which adds it as the last element of the array.
- Then we use sort()with the callback functionfunction(a, b){return a-b}to sort the numbers in ascending order.
- Lastly we return the postion or index of numin the array with theindexOf()function.
Relevant Links
 Intermediate Code Solution:
 Intermediate Code Solution:
Using .findIndex()
function getIndexToIns(arr, num) {
  // sort and find right index
  var index = arr.sort((curr, next) => curr > next)
    .findIndex((currNum)=> num <= currNum);
  // Returns proper answer
  return index === -1 ? arr.length : index;
}
getIndexToIns([40, 60], 500);
Code Explanation:
- First sort the array in ascending order, this is currently done using array functions for minimal footprint.
- Once the array it is sorted, we directly apply the .findIndex()where we are going to compare every element in the array until we find wherenum <= currNummeaning where the number we want to insert is less or equal to the current number number in the iteration.
- Then we use ternary operations to check whether we got an index returned or -1. We only get-1when the index was not found meaning when we get a false for all elements int he array, and for such case, it would mean thatnumshould be inserted at the end of the list hence why we usearr.length.
Relevant Links
 Advanced Code Solution:
 Advanced Code Solution:
by @nivrith
function getIndexToIns(arr, num) {
return arr.concat(num).sort((a,b) => a-b).indexOf(num);
}
getIndexToIns([1,3,4],2);
Code Explanation:
- We use method-chaining to invoke one method after another to solve the problem in a single line. First we merge arrandnumby invoking the arr.concat(num) method
- Then we use sort()with the callback arrow function(a, b) => return a-bto sort the numbers in ascending order
- Lastly we return the postion or index of numin the array with theindexOf()method




