Implement Insertion Sort

Tell us what’s happening:

Your code so far

// I watched the hint, there is one line out of my understanding...
function insertionSort(array) {
  for (let i = 1; i < array.length; i++){
    let curr = array[i];
    for (var j = i-1; j >= 0 && array[j] > curr; j--){
      array[j+1] = array[j];
    }
    array[j+1] = curr;  //why the j can be used here? Does it should be only valid in the inner for loop ?
  }
  return array;
}

// test array:
// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/algorithms/implement-insertion-sort

The variable j is declared in the scope of the inserttionSort array, so it can be referenced anywhere inside it.

that inner for loop is not a local scope? I thought the variable is declared in the scope of the inner for loop…

It is declared in the for loop, but it is local to the function. It is the nature of var. If you use let, then it creates a block scope within the for loop.