# Solution to "Where Do I Belong" Challenge

Hello,

I just solved the “Where Do I Belong” Challenge in the “Basic Algorithm Scripting” section. Could you please critique my code and let me know if this was a good way to solve the problem, or if other methods would be preferable. I’m a noob so go easy on me please. Any feedback is appreciated

Challenge Instructions:

Where do I Belong

Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number.

For example, `getIndexToIns([1,2,3,4], 1.5)` should return `1` because it is greater than `1` (index 0), but less than `2` (index 1).

Likewise, `getIndexToIns([20,3,5], 19)` should return `2` because once the array has been sorted it will look like `[3,5,20]` and `19` is less than `20` (index 2) and greater than `5` (index 1).

My Code:

``````function getIndexToIns(arr, num) {
let sortedArray = arr.sort((a, b) => a-b); //First sort the array and save it to a variable called sortedArray.
let findMyIndex = sortedArray.findIndex(number => {return number >= num}); //Next, find the first index where a given number is equal to or greater than num.
//Save that index number to variable findMyIndex.
if (findMyIndex === -1) {sortedArray.push(num)} //If there is no such number (num is greater than any number in array for instance), push() num into the array.
else {sortedArray.splice((findMyIndex), 0, num);}//Else add num into the array at the index number held by findMyIndex.
console.log(sortedArray)
if (findMyIndex === -1) {return sortedArray.length -1} //If num needs to be pushed onto the end of the array, return sortedArray.lengh -1 to fulfill the task.
else {return findMyIndex} //Else, simply return the value of findMyIndex variable.
}
``````

First of all, you need to learn how to properly indent your code. I will do that below (without all the comments as they are mostly not needed). Making your code more readable to others helps them to review your code more easily. You should only use comments to explain why you do something and not just describe what the code does. If you use descriptive variable and function names your code will not need as many comments.

``````function getIndexToIns(arr, num) {
let sortedArray = arr.sort((a, b) => a-b);
let findMyIndex = sortedArray.findIndex(number => {
return number >= num
});
if (findMyIndex === -1) {
sortedArray.push(num)
}
else {
sortedArray.splice((findMyIndex), 0, num);
}
if (findMyIndex === -1) {
return sortedArray.length - 1
}
else {
return findMyIndex
}
}
``````

Next, the following lines are not needed at all. The challenge does not expect you to actually insert the item into the array. It just needs to know the applicable index.

``````   if (findMyIndex === -1) {
sortedArray.push(num)
}
else {
sortedArray.splice((findMyIndex), 0, num);
}
``````

See if you can figure out the small change that will need to be made to the following `return` statement if the above lines are removed.

``````return sortedArray.length - 1
``````

Congratulations on solving the challenge, that said, starting out you just need to focus on solving the challenges, efficiency and code elegance is something you will attain as you solve more problems. Don’t get too hang up on that. Well don’t take my word for it, solve this challenge a month from now as you continue coding and working your way through the curriculum and your approach will probably be totally different.

Thanks @okibe007 ! That’s what I’m doing right now, just trying to focus on completing the challenges…then later on will work on making my code shorter and cleaner once I get more experience.

Have you done the whole FCC course yet or still working through it? Do you have any tips for someone new to programming to help pick things up quicker?

@RandellDawson Thanks for the reply! I’ll try to remember to indent properly going forward. Was too caught up in getting the code to work to complete the challenge I guess.

Good point about not needing to add the number into the array for this challenge. I’ll eliminate that extra code like you suggest.

And to answer your question, I think that `return sortedArray.length - 1` would need to become `return sortedArray.length` to solve the challenge, since num is no longer being inserted into the array. Is that correct?

1 Like

Still working through the curriculum, as for tips, spend as much time as you need on concepts that aren’t clear to you, the curriculum builds on previous challenges so its important you understand the fundamentals.

if you use a for loop and compare each item in the array to num to find the index then it will be much easier to understand your code for you at this stage

Congrats on solving it. As @RandellDawson said, formatting your code helps other developers and yourself, I would recommend you get in the habit of running the formatter every now and then (you can right-click on the editor and select it from the menu).

If you want a challenge, try to figure out how to solve this without sorting the array (it is possible, and probably the “most efficient” solution).

Thanks @ghulamshabirbaloch, I’ll give that a try! I wasn’t sure if using sort() and findIndex() was more efficient or using a loop. I’ll give it a try with a loop next.

@okibe007 Good advice, I’ll try to do that. Some of the concepts I struggle with are things I realize were covered previously, but I didn’t practice with them enough or just didn’t completely grasp the concept from the start.

@colinthornton You mean something like…this?

``````let greaterNums = 0;
for (let i = 0; i < arr.length; i++) {
if(arr[i] >= num) {greaterNums++};
}
return (arr.length - greaterNums)
}
console.log(getIndexToIns([40, 60, 1, 94, 48, 69], 50));`````````

As @colinthornton stated above, it is possible to solve this challenge without sorting the array first.

The algorithm to do this is quite simple. Just count the number of values in `arr` that are less than `num` and return the count. Take a simple example.

``````const arr = [1, 4, 3]
getIndexToIns(arr, 2) // should return 1
``````

How many of `arr`’s elements are less than the number `2`? There is only `1` number less than `2` (that is the number `1`), so the count is `1`.

``````const arr = [2, 4, 3]
getIndexToIns(arr, 1) // should return 0
``````

How many of `arr`’s elements are less than the number `1`? There are no numbers less than `1`, so the count is `0`.

``````const arr = [2, 4, 3]
getIndexToIns(arr, 6) // should return 3
``````

How many of `arr`’s elements are less than the number `6`? There are `3` numbers less than `6` (they are 2, 4, and 3), so the count is `3`.

See if you can write the function with this algorithm instead.