Basic Algorithm Scripting - Where do I Belong Sorting

Tell us what’s happening:
My code for sorting an array with n^2 complexity doesn’t work …what am i doing wrong here ?

Your code so far

function getIndexToIns(arr, num){
  let a=[...arr,num];
  for(let i=0;i<a.length;a++){
    for(let j=0;j<a.length;j++){
      if(a[i]>a[j]){
        let temp=a[i];
        a[i]=a[j];
        a[j]=temp;

      }
      
    }

  }
  let ind=a.indexOf(num);
  return ind;
}

getIndexToIns([40, 60], 50);

Challenge: Basic Algorithm Scripting - Where do I Belong

Link to the challenge:

Here:

for(let i=0;i<a.length;a++){

I assume you creating your own for loop as sort approach to find the index.

But first for loop only loop once.

Do you mean i++ instead of a++ ?

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.