Where do I belong challenge, passing all but one?

Hi :slight_smile:

I’m having some trouble with this challenge…

My code so far passes all the tests except one (see commented code). Can anyone advice whether my code is legible to begin with? Perhaps the approach I am using is causing the one test to fail.

function sorter(a, b){
  return a - b;
}
function getIndexToIns(arr, num) {
  // sort the array
  // find where num fits in
  // return that index
  console.log(arr.sort(sorter));
  arr = arr.sort(sorter);
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] >= num) {
      num = arr.indexOf(arr[i])
      console.log(num);
      return num;
      }
    }
  return 0;
}

getIndexToIns([2, 5, 10], 15); //fail!
getIndexToIns([40, 9, 60, 3, 64, 9, 8], 50); //pass
getIndexToIns([10, 20, 30, 40, 50], 35); //pass
getIndexToIns([10, 20, 30, 40, 50], 30); //pass
getIndexToIns([40, 60], 50); //pass
getIndexToIns([3, 10, 5], 3); //pass
getIndexToIns([5, 3, 20, 3], 5); //pass
getIndexToIns([2, 20, 10], 19); //pass
getIndexToIns([], 1); //pass

I tried adding an else-if statement where num > arr[i] but that didn’t resolve it. Just need some advice/a hint on what I could do to solve the challenge, it would be much appreciated.

So, in this line:

… You are saying “completely blow away the passed param num and set it to the index.” Is that your intent?

Here’s the thing: if arr[i] >= num, then num simply needs to be set to i – doing arr.indexOf(arr[i]) will return the first time this value is encountered, but if this test passes, it does so only the first time. By returning within the if statement, you break out of the for loop entirely.

If, on the other hand, arr[i] never exceeds num, then you simply want to pass back the last position in the array. How would you find that? :wink:

As to why that one test is failing, versus any other… Is there something unique to that test? I see that there is. Where in the list does that num get inserted? It’s the case of arr[i] never exceeding num. The array isn’t empty, so returning 0 is not valid (that worked for the last case, as the empty array will be adding num at position zero). So what should you return when the number is greater than anything else in the array?

1 Like

Thanks for the explanation @snowmonkey :slight_smile:

The thing I am still trying to get right is the iteration / looping part (in general)… Sometimes you even iterate a function’s parameter inside the loop, and that baffled me for a while.

Making a resolve to work on a challenge until I get it right. So here goes nothing! ^.^

The test that you fail on: getIndexToIns([2, 5, 10], 15) goes thru the whole loop but the comparison you make (arr[i] >= num) will never be true: 10 >= 15 is false. So it then exits the loop and returns 0.

You may want to add another check to see if the array is empty or not and change what you return based on that.

Sure, if the function has passed an array as a parameter (or even a string, which you can process character by character), you can (and often will) iterate over every member of that array or string.

Loops are a tricky business, absolutely. And there are many different forms loops can take, depending on your need. It takes time, and practice.

As I’ve been saying a lot lately, slow down and pull out a piece of paper. Write out the process, step by step, leaving code completely out of it. Break it down as though you were trying to explain the lesson to a six-year-old child with a learning disability. Small steps, clear path, simple logic. At this point, don’t think in code. You may need to loop, but don’t think about the mechanism, simply think about what is driving the loop, what you need to know to determine how many times through the loop, everything in teeny steps.

When you’ve done that, you’ve created the algorithm for the lesson! Then comes the fun bit, turning the algorithm into the code. If you have gone through the algorithm process well, then each little step should be a little code. In some cases, some code will combine steps, and that’s ok.

And lastly (but by no means leastly!!) Comment the BEJABBERS out of your code. Even on little projects like these, comments on nearly every line will help you in the long run. Often, my comments will resemble the algorithm piece that line or loop handles. Doing this keeps the algorithm foremost in your mind, and it gives you a reference of what you should expect at a given point. If the comment and the output (through, for example, console.log()) don’t match, that line might bear scrutiny.

It’s practice, and experience. And breaking things. No worries, @mslilafowler, you’re doing great!

1 Like