FCC Algorithm Where Do I Belong?

Whats up everybody. I wanted to talk about this challenge so I can get a better understanding of it myself. So FCC asks you to add the argument to the lowest index of where it should be sorted. For instance if you have…

getIndexToIns([10, 20, 30, 40, 50], 35). The value of 35 will be sorted to the 3rd index of the array. and it should return a value of 3 once your function is done running. So after reading the appropriate documentation on sort(). and remembering the indexOf() method, I figured I had a pretty good idea on how to solve this challenge.

Now for the reason I’m writing about it… I solve this challenge pretty easily but I can’t comprehend why one way worked versus the other. So at first glance I created a variable called elements to hold my argument which is positioned in index 1.(arguments[1])
Then I knew I needed to push the element into the array. Lastly, I said it’s time to sort and return the index of my elements variable. So here’s what it looked like….

function getIndexToIns(arr, num) {
var element = arguments[1];
return a-b;
return arr.indexOf(element);

Now for some reason this code is wrong. I know the right way to do it but the most important part I’m trying to understand is why. After attempting to test the code by return arr; It returned a value of 3??????? To my lack of understanding I thought it should of returned the new array. But anyway I easily cleaned this up by attaching my statements to a few variables and Walla Congratulations Gamification makes me feel good again. Anyways here is the finished code. Enjoy

function getIndexToIns(arr, num) {

var element = arguments[1];
var array = arr;
function compareNumbers(a, b) {
return a – b;
var newArr = array.sort(compareNumbers); return newArr.indexOf(element);
}getIndexToIns([40, 60], 50);

2 Replies to “FCC Algorithm Where Do I Belong?”

  1. You have a ‘))’ in your sort function, one of those should be after your function call before the ‘;’.

    Also when you post code if you put” and then the matching ” it will do code formatting for you. You may have to use the ‘text’ editor instead of the visual editor for posting though.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s