Getting an infinite loop for simple function

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);

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.

markdown_Forums

2 Likes

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.

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

1 Like

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