# Where do I belong - Help

Where do I belong - Help
0

#1

Hello,

I’ve try to solve this challenge like this

``````function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr.sort();

for (i = 0; i < arr.length; i++) {
if (arr[i] <= num <= arr[i + 1]) {
return(arr.indexOf(arr[i]));
}
}
}
``````

For an unknow reason to me it didn’t work but I think the logic should work right ?

I’ve check the hint and found that logic even more clear, I managed to redo it by myself but I would still like to understand why the first code didn’t work

``````  arr.sort();

var final = 0;
for (i = 0; i < arr.length; i++) {
if (num > arr[i]) {
final += 1;
}
}
return final;
}
``````

#2

On first inspection:

Sort doesn’t sort numerically - It sorts by Unicode points.

In order to get it to sort the way you want, you need a function inside of the sort method. Something like this:

arr.sort(function(a, b) {
return a-b;
}

(Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)
You may also need to store that in a new array. I can’t remember. But yeah that should help.

#3

Er. forgot a parenthesis.

arr.sort(function(a, b) {
return a-b;
});

#4

That is the reason why first code works incorrect:

JavaScript interpretator reads this expression not same as human, you need to rewrite as 2 statements (a <= b) && (b <=c), the (a<=b<=c) not allowed (that would work eg. in Python but not here in JavaScript)

UPD And how zealousAnemone said, maybe it sorts incorrect - try to console.log(arr.sort()) to see if there is a problem here

#5

There is the first issue as mentioned by @zealousAnemone to consider.

Also, the issue that @keith1111 mentioned is the main reason it did not work. Using the following call to the function as an example, during the 1st iteration of the for loop, variable i = 0, so arr[0] = 10 and arr[0+1} which is arr[1] = 20. Your if then statement condition evaluates to false, because unlike Python, I believe (could be wrong) JavaScript is evaluating each individual comparison before the next comparison (going from left to right).

``````getIndexToIns([10, 20, 30, 40, 50], 35)
``````

So first the expression arr[0] <= num evaluates to true, because 10 <= 35. Next, JavaScript evaluates true <= arr[1] (arr[1] is 20) as true. Why does true <= 20 evaluate to true? Because the 20 gets coerced into a Boolean value because of the logical comparison being made. So, since true <= true evaluates to true, the function exits and returns 0 because arr.indexOf(10) is 0.

NOTE: If I am wrong about my assumptions here, I hope someone will correct me. This is just my guess based on my limited knowledge of how JavaScript works.

#6

Thanks a lot zealousAnemone, keith1111 and randelldawson, I’m going to test with thoses changes

I’m starting to think that freecodecamp challenges are not enough, there are things that I use without fully understanding how they work. Did you experience the same feeling ? Do you advise specific sources to fill the gap ?