# Where do I belong? No. Seriously! [solved]

Where do I belong? No. Seriously! [solved]
0

#1

my code should work. and it does.
except on the last test.
which is weird.
for some reason (unicode blah blah) the `.sort` thinks 10 is less than 2.
but it’s even weirder because it doesn’t think 15 is greater than 10 or 5 or 2, but it knows that 19 is greater than 10 and 2 in the earlier test.

``````function getIndexToIns(arr, num)
{
var result = 0;
var count = 0;
var newArr = arr.sort();
for (i = 0; i < newArr.length; i++)
{
if (num > newArr[i])
{
count++;
}
else
{
result = count;
}
}
console.log("arr = " + arr + " | newArr = " + newArr);
return result;
}

console.log(getIndexToIns([10, 20, 30, 40, 50], 35));
//should return 3.
console.log(getIndexToIns([10, 20, 30, 40, 50], 30));
//should return 2.
console.log(getIndexToIns([40, 60], 50));
//should return 1.
console.log(getIndexToIns([3, 10, 5], 3));
//should return 0.
console.log(getIndexToIns([5, 3, 20, 3], 5));
//should return 2.
console.log(getIndexToIns([2, 20, 10], 19));
//should return 2.
console.log(getIndexToIns([2, 5, 10], 15));
//should return 3.
``````

https://www.freecodecamp.org/challenges/where-do-i-belong

#2

In your example, 10 is less than 2 w/r/t sort, by default it’s going to sort lexicographically, like in a dictionary

#3

so do i need to int.parse?
also what is w/r/t?

#4

Sorry, With Respect To.

And nope, you don’t need to parse anything, you just need to give sort a callback function. You always need to do this if you’re sorting numbers: to be honest the only time you don’t is if you want an array of things to be sorted in the order of the character codes for their string representation (0-9, then A-Z, then a-z etc).

If you look at the examples on MDN, there are a few numeric ones https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort; return `a - b` in the callback, anyway I think, I always end up checking anyway, it never sticks in my head.

Default sort is pretty useless, is the main thing you should take away. It has to be - the are a variety of ways people need to sort things and a variety of things they want sorting, so that’s what the callback’s for; along the lines of `arr.sort((a,b) => a - b)` or whatever

#5

thanks. this led me down a path. where i replaced
`var newArr = arr.sort();`
with
`var newArr = arr.sort(function(a, b) {return a-b});`

and now the numbers sort properly.

but i’m still getting the same result on the last test.

#6
``````function getIndexToIns(arr, num)
{
var result = 0;
var count = 0;
var newArr = arr.sort(function(a, b) {return a-b});
for (i = 0; i < newArr.length; i++)
{
if (num > newArr[i])
{
count++;
}
result = count;
}
return result;
}

console.log(getIndexToIns([10, 20, 30, 40, 50], 35));
//should return 3.
console.log(getIndexToIns([10, 20, 30, 40, 50], 30));
//should return 2.
console.log(getIndexToIns([40, 60], 50));
//should return 1.
console.log(getIndexToIns([3, 10, 5], 3));
//should return 0.
console.log(getIndexToIns([5, 3, 20, 3], 5));
//should return 2.
console.log(getIndexToIns([2, 20, 10], 19));
//should return 2.
console.log(getIndexToIns([2, 5, 10], 15));
//should return 3.
``````

figured it out

my `else {}` was unnecessary.