Where do I Belong

I need help with this problem. I feel like prior to the Basic Algorithm Scripting section, freecodecamp held my hand with detailed explanations to problems and examples. Now that they stopped doing that, it feels like I did a bad job of retaining all of this new information. Outside of HMTL, CSS, and writing the start of a for loop…I basically feel lost. This is all I have for this problem:

function getIndexToIns(arr, num) {

const array1 = [40, 60, 50];
array1.sort();

console.log(getIndexToIns([40, 60], 50));

I’m trying my best to not just look up the answers.

1 Like

It’s easier if you start with a plan.

Can you explain how you would do this entirely by hand, with zero code, if I gave you a list of unsorted numbers written down on a piece of paper and asked you where a new number would go in the list if the list was sorted?

2 Likes

With zero code, if you asked me where a number would go if the list were sorted, I would tell you 50 needs to go in the middle of 40 and 60. I would tell you that is the second spot, but of course in an array the 1st spot is actually 0 so we would be placing 50 in the 1st spot.

So my guess is that I need to get the numbers in the correct order, then return the value of the spot the number outside of the array ends up. I just don’t know how to go about doing this.

But how do you know that (there’s a purpose, I promise)

How would you tell a elementary school kid that knows their numbers what to do. Write down a list of steps for them to follow. Remember to be specific, because this elementary school kid hasn’t done this before.

Assuming the kid knows his or her numbers, I would tell them that the array needs to start with the smallest number and end with the largest number. And therefore, that is how you know 50 goes in the middle, right?

That’s not a list of steps for them to follow though.

Step 1:

Step 2:

Step 3:

Computers are like stupid children with literal rocks in their brains. We need to make very literal lists of steps for them.

Ahhhh I see.

Step 1: Start array with smallest number

Step 2: End array with largest number

Step 3: Place number outside of array in correct spot (smallest - largest)

Step 4: Indicate where that number you placed inside the array is and return that number

There we go!

Your sort can be used to put the numbers in the right order. (Gotta be careful with sorting numbers JavaScript Array Sort – How to Use JS Sort Methods (With Code Examples))

So the question becomes, how do you tell where the new number goes if you already have a sorted list.

Where does 8 go if I have the list 2, 5, 7, 10? In spot 0, 1, 2, 3, 4, or 5? How can I tell if spot i is right?

8 would go in spot 3.

If you tell the computer that the beginning of the array is spot 0. Then you should be able to ask the computer what spot is 8 in…then it will spit out the number 3. In this case, the number outside of the array is num…so you would have to write something that finds what spot num is in…inside the array

Right. You’re thinking along the right lines.

Let’s make this into smaller parts again

It can’t be spot 0 because 2 ? 8

It can’t be spot 1 because 5 ? 8

It can’t be spot 2 because 7 ? 8

It is spot 3 because 10 ? 8

Sorry I am confused on what you’re asking me to do now.

So I would write multiple lines of code to indicate what is false and which one statement is true

Fill in the statements I made. What can you put in place of the ? to explain how you know where to put the 8

Step 1: If index (i) equals 0 then it must equal 2 and not 8 (because 2 < 8)
Step 2: If index (i) equals 1 then it must equal 5 and not 8 (because 5 < 8)
Step 3: If index (i) equals 2 then it must equal 7 and not 8 (because 7 < 8)
Step 4: If index (i) equals 4 then it must equal 10 and not 8 (because 10 > 8)

Therefore since the array is 0-4, 8 must be in spot (i) 3.

This is sounding like a loop with some sort of if statement

function getIndexToIns(arr, num) {

const array1 = [40, 60, 50];
array1.sort();

for (let i = 0; i < arr.length; i += 1) {
if (40 < num) {
i = 0;
} if (60 > num) {
i = 2;
} if (50 = num) {
i = 1
}
return num[i];
}
}

console.log(getIndexToIns([40, 60], 50));

This makes sense to me but I know I am doing something wrong. I wrote everything we broke down into simple terms as a for loop

Hmm, a loop over an array usually only considers one array element at a time, not all of them

function getIndexToIns(arr, num) {

arr.sort((a, b) => a - b); 

for (let i = 0; i < arr.length; i += 1) {
  if (arr[i] <= num) {
    return i; 
  }
  return arr.length;
}
}

Instead of doing multiple and using specific numbers I changed it to only do one, but the return arr.length is giving me 0 when it should be 1

I also looked up how to properly use the sort() method

I think this bit is very close but a bit off.

Do you want to stop as soon as one element is less than num?

function getIndexToIns(arr, num) {

arr.sort((a, b) => a - b);

for (let i = 0; i < arr.length; i += 1) {
if (arr[i] > num) {
return i;
}
return arr.length;
}
}

console.log(getIndexToIns([40, 60], 50));

Changed it but it’s now returning 2. Please correct me if I am wrong but this is how I am reading the code I have right now.

  • It starts off sorting the array and letting the computer know the array should go from lowest to highest number.
  • Then the for loop checks everything in that array…going up by 1.
  • Then you say if arr[i] (number inside of array) is > num (number outside of array)…then return the i that num would be in

Try applying standard formatting here:

function getIndexToIns(arr, num) {
  arr.sort((a, b) => a - b);

  for (let i = 0; i < arr.length; i += 1) {
    if (arr[i] > num) {
      return i;
    }
    return arr.length;
  }
}