FCC : Where do I belong

So I have the following code for this challenge:

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

I’m stuck on the empty array scenario.
I’ve also tried using the push method to push the num in arr if arr.length === 0 and then with no luck doing a return arr.indexOf(num)… any other tips/tricks?
On a side note, can you use “switch” statements in functions?

1 Like

If the array is empty what is the lowest index? There are not many options.

Regarding switch you can use it in functions.

zero right? but doesnt arr.length === 0 imply the array is empty?

Yes, the array is empty. So the number you are going to add will be the first one, i.e. at the index 0.

correct. but am I wrong to skip the adding or using “push” method and just return 0?

im getting undefined when I enter either of the below codes:

else if (arr.length === 0){
            return 0;
        }

else if (arr.length === 0){
            arr.push(num);
            return arr.indexOf(num);
        }

Because if array is empty your for loop doesn’t execute.

1 Like

thank you!!

  function getIndexToIns(arr, num) {
    arr.sort((a, b) => a - b)
    if (arr && arr.length <= 0){
        arr.push(num);
        return arr.indexOf(num);
    } else {
        for (let i = 0; i < arr.length; i++){
            if (num === arr[i]){
                return arr.indexOf(num);
            } else if(num < arr[i]){
                arr.splice(arr.indexOf(arr[i]), 0, num)
                return arr.indexOf(num);
            } else if (num > arr[arr.length - 1]){
                arr.push(num);
                return arr.indexOf(num);
            } 
        }
    }
}

this works! is there too much code though for the actual challenge… thoughts?

Why not just pull the condition out of the for loop to the top of the function just after the sort method.

You can check hints to see alternative solutions.

which condition is that?

Never mind. You did that already.
I was just wondering why u did if arr and i am not sure the length can be less than zero - an empty array will return true… Seems redundant

the third line in my code was the only way I could think of to express the condition if the array was empty… is there another way to do so??

thanks Randell for the insight. Helpful!

Hello everyone!
I know it’s been done before, but I’m just a noobie coding.
So, here’s my humble two cents contribution…

function getIndexToIns(arr, num) {
  arr.push(num);
  arr.sort(function(a, b){return a - b});
  return arr.indexOf(num);
}
getIndexToIns([40, 70, 60], 50);

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.