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.
}
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?
@camperextraordinaire 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?
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 @camperextraordinaire 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.