Oletem
1/10/2018 - 3:44 PM

## where do I belong

``````:beginner: 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;
}
:rocket: Run Code184

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.
parseInt()82
:beginner: 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 (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);
:rocket: Run Code29

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.
:beginner: 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);
:rocket: Run Code21

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.
:sunflower: Intermediate Code Solution:
by @faustodc

function getIndexToIns(arr, num) {
arr.push(num);
arr.sort(function(a, b){return a-b});
return arr.indexOf(num);
}
:rocket: Run Code41

Code Explanation:
First we add the number num to the array using push() which adds it as the last element of the array.
Then we use sort() with the callback function function(a, b){return a-b} to sort the numbers in ascending order.
Lastly we return the postion or index of num in the array with the indexOf() function.
push()7
sort()27
indexOf()38
:sunflower: 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);