JavaScript Nested Loops: For & While


#1

https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/web-dev-intermediate-javascript-practice/lessons/intermediate-javascript-coding-challenge/exercises/fix-the-broken-code

Hello!
Ref. exercise Nº 5, “Fix Broken Code”, in the end of Module 5, course Web Development.

In the instruction, what does it mean : 'push in the smallest power of two which is greater than the current elemen’.?

The solution is not clear, based on given lesson and training… Very confusing.

Could someone explain the relationship between inner and outer loops ?

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

const smallestPowerOfTwo = arr => {
let results = ;
for (let i = 0; i < arr.length; i++) {
number = arr[i];

        let k = 1
        while (k < number) {
              k = k * 2;
        }
        results.push(k);
  }

  return results

}

console.log(smallestPowerOfTwo(numbers))

PRINTS: [8, 4, 16, 32]


#2
2^1 = 2; 
2^2 = 4; 
2^3 = 8; 
etc.

These are the power of 2

So if the current element is 3 the smallest power of two which is greater is 4 and if the element was 5 the smallest power of two which is greater would be 8.


#3

Great, I know how we came up with 8 (2 x 2 x 2).

But how did we come up with Print: [8, 4, 16, 32] ?


#4

The push method adds an item to the end of an array. So whatever the value of “k” is when it reaches this line in the code gets added to the “results” array.

At the end of this function the results array gets returned.

And this line logs the returned value of the smallestPowerOfTwo function to the console, which in this case is the “results” array.


#5

I am sorry, I still do not understand your comment:

"if the current element is 3 the smallest power of two which is greater is 4 and if the element was 5 the smallest power of two which is greater would be 8."

And I can not see how the inner WILHE loop processed a “power of two” operation and calculated final result: [8, 4, 16, 32].


#6
const smallestPowerOfTwo = arr => {
  let results = [];
  for (let i = 0; i < arr.length; i++) {
    number = arr[i];
    i = 1;
    while (i < number) {
      i = i * 2;
    }
    results.push(i);
  }
  return results
}

What is wrong with the above code?


#7

Hi. I can not figure out the solution based on the exercise instruction. I do not understand what 'current element" or “smallest power of two” mean in the context:

" We loop through the argument array and push in the smallest power of two which is greater than the current element."

I do not see how the code gets to result: [8, 4, 16, 32]

        let j = 1;
        while (j < number) {
              j = j * 2;
        }
        results.push(j);
  }
  return results

#8

Examine the list one item at a time. What power of 2 is greater than that number?

5  ?   8   =>  2 ** 3
3  ?   4   =>  2 ** 2
9  ?  16   =>  2 ** 4
30 ?  32   =>  2 ** 5

#9

Ok, great, I finally got what the function is trying to do.

I am still confused with the inner while loop. Why let j = 1 ? why not let j = 0, or = 2 ?
I runned the code with 'let j = 0 , it keeeps runing forever and does not return any result.
I runned the code with 'let j = 2 remains same results for 1. And runned for 3, and returns an array [6, 3, 12, 48]. So what does (let j = 1) mean ?

 let j = 1;
    while (j < number) {
          j = j * 2;
    }
    results.push(j);

}
return results


#10
0 * n === 0  for all values of n

We don’t want that, because then all the multiplication will result in zero.

Starting with 1 allows the smallest power of two greater than 1 to be 2, which means 1 is a valid number to include in the test list. If we start with 2, then a value of 1 in the list will result in an incorrect outcome.


#11

I assume value 1 (let j = 1), is merely simbolic to make the while loop operable, since 2 returns same value of 1.

And I see “j” as a ‘2’, which will continue to multiply by 2 ([j = j * 2] = power of 2) until it reaches the next value that is greater than the current element, and returns that value.

1º element is 5, than:
2 * 2 * 2 = 8. Nº 8 is the returned value

2º element is 3, than:
2 * 2 = 4. Nº 4 is the returned value, and so on.

Tks!!


#12

You’re getting the idea very well. Given a starting value of 1, we double it with each iteration up to a limit, that being the doubled value is greater than the input; i.e., x = 5, y = 2 ** n | n is INT, z = y > x. If z return y.