Getting an infinite loop for simple function

Getting an infinite loop for simple function
0

#1

function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr.sort(function(a,b){
return a-b;
});

for(var i=0;i<arr.length;i++){
if(num < arr[i]){
arr.splice(i,0,num);
}
}
return arr;
}

getIndexToIns([40, 80 ,60], 50);


#2

OK, so the code you posted is part of a function. We need to see the function declaration with any parameters used. Also, where is the line of code which calls the function? If this is an FCC challenge, then what is it’s name? You posted:

// missing function declaration such as function unknownFunction(paramter1, parameter2) {
  arr.sort(function(a,b){
    return a-b;
  });

  for(var i=0;i<arr.length;i++){
    if(num < arr[i]){
      arr.splice(i,0,num);
    }
  }
  return arr; 
// missing close of function ( a curly bracket } )

// missing a call to the function such as unknownFunction([1,2,3,4,5], 3);

#3

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard.


#4

Hi, i’ve changed the post. This is Basic Algorithm scripting challenge"Where do I Belong?". But i modified the code to return the array rather than index and it’s giving an infinite loop at if condition.
Thank you for your help.


#5

If below is what you have, then read my comments below the code for help understanding why you are getting the infinite loop message. I explain in my comments what is happening as the code executes.

function getIndexToIns(arr, num) {
  arr.sort(function(a,b){
    return a-b;
  });

  for(var i=0;i<arr.length;i++){
    if(num < arr[i]){
      arr.splice(i,0,num);
    }
  }
  return arr; 
}

getIndexToIns([3, 10, 5], 3);

After the arr.sort line the array is becomes [3, 5, 10]

for loop with variable values explained:

1st iteration - i = 0, num = 3, arr[0] = 3
if statement - 3 < 3 evaluates to false so splice does not occur

2nd iteration - i = 1, num = 3, arr[1] = 5
if statement - 3 < 5 evaluates to true so enter splice code
arr.splice - arr.splice(1, 0, 3) results in arr becoming [ 3, 3, 5, 10 ]

3rd iteration - i = 2, num = 3, arr[2] = 5
if statement - 3 < 5 evaluates to true so enter splice code [ 3, 3, 3, 5, 10 ]

As you can see, the array is going to keep having a 3 insert before the 5
and the array will keep growing larger and larger because the for loop exit
condition of i < arr.length will always evaluate to true

If you do not understand what is happening with the splice you are using,
the review the documentation here before continuing.

Also, remember the challenge instructions ask you to return the index of where num would go.
You are currently returning the array itself.


#6

Thank you for helping me. I missed the part that array length is increasing.


#7

Yup the length has to increase or the script will loop forever (well until it’s terminated by closing the tab executing the javascript).