JS, Where do I Belong - Extra Question!

Tell us what’s happening:
Describe your issue in detail here.

Hi, freeCodeCamp community! I did end up solving this question, but I got a little distracted and wanted help solving one other additional idea/question I had as I was working through this problem.

I have passed the challenge, but if you want to stick around and give me some help to answer my own curiosity, I’d totally appreciate it!

I had originally misread the code challenge and instead of reporting back in the function where to insert the number argument we passed into the function, I thought we actually had to make a new array, as copied from the one passed into the function, and then stick the new number in the correct order inside of the array.

I ran the console.log method on my numbers for fun and it almost words. Can you take a look at my code and help me figure out what to edit or type so that my new copy of the array would be in correct sorted order, and it would actually include that new number argument we’re passing through?

Check out my code below and let me know what you think. I’m just learning, I appreciate the help! Thanks!!

  **Your code so far**
function getIndexToIns(arr, num) {
const arrCopy = arr.slice();
arrCopy.sort(function(a, b){return a - b});
for (let i = 0; i < arrCopy.length; i++) {
  if (num > arr[i]) {
    arrCopy.splice(arr[i], 0, num);
  }
  return arrCopy;
}
return console.log(arrCopy);
}

console.log(getIndexToIns([4, 6], 5));

*And this results back [ 4, 6, 5 ] in the console.log !

  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Where do I Belong

Link to the challenge:

Why not use the algorithm you used to solve the challenge and use the index you would have returned, as the first argument of the splice method. The splice would take place after you get the index but before you return the final version of arrCopy.

function getIndexToIns(arr, num) {
  const arrCopy = arr.slice();
  // remaining logic you wrote to get the correct index of where to insert num when you passed the challenge
  arrCopy.splice(index, 0, num);
  return arrCopy;
}

Hey there, thanks so much for getting back to me!

I tried that, and unless I’ve misunderstood what you mean by index, it doesn’t give me back the new array with the num = 5 correctly inserted into it.

Here’s my code…

function getIndexToIns(arr, num) {
  const arrCopy = arr.slice();
  arrCopy.sort((a, b) => {return a - b});
  for (let i = 0; i < arrCopy.length; i++) {
    if (arr[i] >= num) {
      arrCopy.splice(arr[i], 0, num);
    }
    return arrCopy;
  }
  return console.log(arrCopy);
}

console.log(getIndexToIns([4, 6], 5));```

So right now the console.log reports back [ 4, 6 ].

Maybe I'm missing something. Can you help me find the missing link or logic step I'm not taking?

Thanks again!!

You said in your first post that you solved the challenge already, so you already know how to get the correct index. Where is that code? Use the code you used to actually solve the challenge to get the index and then use what I showed above to insert num into arrCopry before returning arrCopy.

Got it, I’ll go revisit that - I think I’ve been staring too long at the same code and not typing something correctly! Thanks again.