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: