# Where do I belong challenge

If anyone could help me figure out what is wrong with my code. It works for some of the tests but not the others. Thanks!

``````function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr = arr.sort(function compare(a, b){return a - b;});
index = 0;
for(var x in arr){
if(x <=num){
index = x - 1;
}
}
return index;
}

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

Hey there Nick,

Really nice code so farā¦ but I think you are doing something I do far too oftenā¦ and overthinking it

1. Try to concat arr and num, so you have one nice neat array.
2. Then apply your arr.sort function.
3. Then lastly, use indexOf to find the num from your sorted function.
• hintā¦ no loops needed Hope this helpsā¦ let me know if you have anymore questions! And keep up the awesome work!
4 Likes

I tried but didnāt workā¦

``````function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr = arr.sort(function compare(a, b){return a - b;});
for(var x in arr){
if(arr[x] <= num){
index = x - 1;
console.log(index);
}
}
}

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

This is where Iām at now and itās still only passing some of the tests.

``````function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr = arr.sort(function compare(a, b){return a - b;});
index = 0;
for(var x in arr){
if(arr[x] >= num){
index = x - 1;
}
}
return index;
}

getIndexToIns([5, 3, 20, 3], 5);``````

That way it will take into account that indexes start at 0.

No, in fact you use āx - 1ā because your condition works twice as the array contains two elements which are >= num. Thatās wrong way, think how you can avoid it.

Sorry for the delayā¦ if you are still have ???'s try reading my first comment again, and see if your code follows those steps. For more detail, check out my example below :

## num = [9];

//Notice- arr is unsorted, and does Not include ā9ā

## num = [9];

//even though we add num to arr, num is still equal [9];

//You need to concat, so num will now show in arr, then sort, then find the indexOf 9 within arr.

*** Summary:** How do you make this problem one big array containing arr and num? How would you sort the array? How do you find the ānumā within the array?

Thank you for all of your help, I know this code works because when I run the test it returns the correct value, although it says its not passing any of the tests even though itās returning the correct value. What should I do?

``````function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr.push(num);
arr = arr.sort(function compare(a, b){return a - b;});
for (var x in arr){
if(arr[x] == num){
return x;
}
}
}

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

Hey there

just finished this one without any problems. However, I much appreciate your thoughts on elegance and efficiency.

``````function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr.push(num);
arr.sort(function(a,b){
return a -b;
});
return arr.indexOf(num);
}

getIndexToIns([40, 60], 50);
``````
2 Likes

Awesome Sauce!!! You did great! I knew you would get it!

That was almost my same answerā¦ I just used concat instead of pushā¦ But same result. Once you get through the advanced Bonfires, and take a look back at theseā¦ you wonāt believe how much you have learned!

Did you work out a solution? Have you tried your code without the for loop (I think indexOf does what youāre trying to do there)?

Thatās amazing. I came up with a very similar solution. Nice, short, and simple.

function getIndexToIns(arr, num) {
// Find my place in this sorted array.
function compareNumbers(a, b) {
return a - b;
}

arr.push(num);
var arr2 = arr.sort(compareNumbers);
return arr.indexOf(num);
}

getIndexToIns([3, 10, 5], 50);

1 Like

And if you want to see the ugliest code ever that passes the test then here ya go.

function getIndexToIns(arr, num) {
function compareNumbers(a, b) {
return a - b;
}
// Find my place in this sorted array.
var sortedArray = arr.sort(compareNumbers);
var position;
console.log(sortedArray);
if(num<=sortedArray[0]){
position=0;
}
else if(num>sortedArray[sortedArray.length-1]){
position = sortedArray.length;
}
var i=0;
while(i<sortedArray.length){
if(num>sortedArray[i] && num<=sortedArray[i+1]){
position = i+1;
}
i++;
}
console.log(position);
return position;
}

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

1 Like

Nice idea. One of the things Iām enjoying so far is looking up the different rationales people find to solve the challenges.

Its been a while nice to see youāve come up with a very similar solution!

you can do it like this, it is less verbose

function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr.push(num);
arr.sort(function(a,b){return a-b;});
return arr.indexOf(num);
}

getIndexToIns([40, 60], 50);

This is why Iāve had so much fun learning this stuff. I try and try until I finally come up with a solution that worksā¦ then I come on here and see the solutions others have posted that approach the problem in a completely different way to get the same result. Iām picking up on things, but these forums are definitely helping me find where I could clean up my code and/or shorten things up. What I came up with is a lot longer than some of the other solutions, but works:

function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr.sort(function(a,b){
return a - b;
});
var last = arr[arr.length-1];
var first = arr[0];
if (num <= first) {
return 0;
}
if (num > last){
return arr.length;
}

for (i=0; i<arr.length; i++){
var lower = arr[i];
var upper = arr[i+1];

if (num >= lower && num <= upper){
return i+1;
}}}

getIndexToIns([10, 20, 30, 40, 50], 30);

Thanks for the hint, I rewrote my solution, using indexOf instead of a for loop makes the solution a lot cleaner. I made one change though, instead of using concat, I pushed the number into the array.

function getIndexToIns(arr, num) {
function compare(a,b){
return a-b;
}
arr.push(num);
arr.sort(compare);
console.log(num);
num = arr.indexOf(num);
return num;
}