Where do I Belong what is the wrong

Where do I Belong what is the wrong
0

#1

function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  arr.push(num); // i want to make it array.
  for(var i =0;i<arr.length;i++){   /// then rearrange it . 
   if (arr[i]>arr[i+1]){
        arr[i] = arr[i+1];
        arr[i+1]= arr[i];
   }
   return arr;
  }
  return arr.indexOf(num); then get it index
}

getIndexToIns([40, 60], 50);

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/where-do-i-belong


#2

Here, lets say arr[i] is 11, and arr[i+1] is 10;
It goes inside if condition,

then arr[i] which was 11 is now changed to the value of arr[i+1], which is 10,
So, arr[i] = 10 and arr[i+1] = 10.

now, for the next statement/LOC,

the values on both sides of the assignment operator are the same, hence nothing happens here.
To avoid such scenarios, you can make use of a temporary variable, like

temp = a;
a = b;
b = temp;

Hope this helps :slight_smile: