-
Notifications
You must be signed in to change notification settings - Fork 0
/
where-do-i-belong.txt
35 lines (26 loc) · 956 Bytes
/
where-do-i-belong.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function getIndexToIns(arr, num) {
arr.sort(function compareNumbers(a, b) {
console.log(a,b)
return a - b;
}); // first compare the difference then sort, thats why comparer function is passed inside sort
//console.log(arr);
let position = 0
for(position; position < arr.length; position++)
{
//console.log(position);
if(num == arr[position])
{
return position;
}
else if(num < arr[position + 1])
{
return position + 1;
}
}
return position;
}
getIndexToIns([10, 20, 30, 40, 50], 35)
/*
The comparison function compareNumbers subtracts b from a and returns the result. This indicates that the array elements should be sorted in ascending order. If the result is negative, a comes before b in the sorted order. If the result is positive, b comes before a. If the result is zero, the elements are considered equal and their relative order doesn't change.
order change is done by sort
*/