Hey everyone,
So I am having some trouble understanding how this inner loop is working.
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
j = 1;
while (j < number) {
j = j * 2;
}
results.push(j);
}
return results
}
I understand what the question is asking for and got the answer correct. I just can’t figure out how the nested loop is working.
Could someone help me understand how the inner j loop is looping through numbers without having like a j++ and how it’s checking if it’s the lowest power?
Thanks a bunch!