Fix the Broken Code!

Question regarding the Fix the Broken Code exercise. Below is my code and it is working fine. My question is around the value of j as the code runs. As I understand it the nested while loop keeps running so long as j is less than arr[i], so for the first value in the numbers array, 5, it runs three times taking j from 1, to 2, to 4, and then finally 8 when the while loop stops because the condition is not met anymore and we push the value of 8 to the results loop. Does the value of j then reset back to 1 once the parent loop moves on to look at the next value of 3 in the numbers array. I guess it must otherwise it would not work? And does that mean that all value changes to j take place solely inside the nested while loop? Kind of like what happens in Vegas stays in the nested loop?

const numbers = [5, 3, 9, 30];

const smallestPowerOfTwo = arr => {
      let results = [];
      // The 'outer' for loop - loops through each element in the array
      for (let i = 0; i < arr.length; i++) {
            number = arr[i];

            // The 'inner' while loop - searches for smallest power of 2 greater than the given number
            let j = 1;
            while (j < number) {
                  j = j * 2;
  
            }
            results.push(j);
      }
      return results
}

console.log(smallestPowerOfTwo(numbers)) 

Does this help solve your doubts?

Think block scope and where the let j declaration is written. Technically that variable ceases to exist when the outer loop iterates to the next value for i. Re-entering the loop block is akin to calling a function. The namespace within the block always starts fresh. That is why we can use let repeatedly. Looking at inline code, this would throw an error.