FAQ: Code Challenges: Intermediate JavaScript - Fix The Broken Code!

Hello, I was also a little confused at this lesson, so after I figured it out, I want to share what I understand with this explanation of how it works. Hope it’s understandable.

i is set to 1, if the number is lower than 1 (when the while loop checks it), it will not run the loop, and there for the value stays at 1 because noting is changing the value.

i = 1;

This loop runs until the number is less than i, so it means the function will stop when we get to the smallest Power Of Two that’s greater than the number. Meaning, at the last loop iteration (because the number is still bigger than i, but is about to be less when the loop finishes running), i gets bigger than the number, then it stops. Giving the final result.

// number = 5
 while (i < number) {// this stops because i = 8 which is bigger then number = 5, making the condition false
     i = i * 2; // i = 1, after the first loop, 2, the second, 4,
              // and last loop is 8. 

     console.log(i); // 2, 4, 8
}

Hello team!
I was wondering about this piece of code:
number = arr[i]; without using let,const or var.
Please explain this feature in the code, it does show any error on the compiler.
Also I checked creating a variable and assigning a value like:
VariableName=5 (works correct but I don’t know the reason why).

creating a variable is possible without any of the keywords, but not recommended. It will make the variable global, always

I add a condition to the inner loop to satisfy the logic “calculate the smallest power of two which is greater than the current element”.

So instead of :
while (x < number) {…}

I would change to:
while (x < number || x === number) {…}

Example:

  1. const numbers = [5, 3, 9, 30] → [ 8, 4, 16, 32 ]
  2. const numbers = [8, 3, 9, 30] → [ 16, 4, 16, 32 ] // as (Math.pow(2,4) = 16) > (Math.pow(2,3) = 8)
  3. const numbers = [8, 2, 42, 15, 30] → [ 16, 4, 64, 16, 32 ] // for 42, (Math.pow(2,6) = 64) > 42 > (Math.pow(2,5) = 32)
while (x <= number) {...}

Won’t that add one more iteration, and therefore produce the wrong result?

Thanks, mtf! I, somehow, ran into an error at some point in the past, can’t remember which version or which software i had used that kept throwing errors so I stop using that and didn’t think of trying it again till you mentioned it!
It surely is more elegant and simpler than the additional, separated logics!

1 Like

Can you explain where number = arr[i] comes from after the first for statement?

Is it a variable declaration? I don’t see it stated anywhere earlier, so I’m confused where this bit of code comes from.

Thanks in advance for your help!

It’s simply a named variable to hold the current value from the array.

We could rewrite the loop as,

for (let number of arr) {

}

which actually fixes the broken code, though I don’t think the idea is to change that line in this exercise.

I have no idea what the code is even doing to understand what it is asking me to do.

Hint, there is a nested loop. That means two control variables. Can you identify them? Do you see anything amiss?

This is a lot simpler than it looks. The hint that came with the challenge states that we are dealing with a scope bug and also gave us a clue what variable may be causing the problem which is variable (i).

Running the original code returns just [8] because the “i” variable which is local to the for loop but global to the inner while loop will get update to a value of 4 by the while loop and returned to the outer for loop for the next iteration but because it’s value is now 4 which meets the stopping condition in the out loop, the loop closes and we dont have any other element in the new array by the initial 8 from the very first outer loop iteration.
MY solution:
All i did was give the while loop a new variable j =1 instead of i=1 and bingo.


Hope this helps explain the SmallestPowerOfTwo challenge!
You may need to review the while loop.

Hey, the solution to this is wrong. I’ve been stuck with the right solution wondering why it’s not working lol.

The while loop should state that <= because the power of two could be equal to the number, in which case it would break down, as it would not be able to print a value “greater than the previous”, instead it would return a value equal to the previous.

There is a strange issue. I fixed the code and it looks like this:

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 (true) { const power = Math.pow(2, j); if (power > number) { results.push(power); break; } j++; } } return results }

The error I get is - Your function should work on any array of numbers, not just the numbers array.

Is it a bug? I have tried checking with various arrays and it seems to me like tests are written a little bit wrong. Or am I missing anything?

Welcome to the forums!

Because the link to the exercise on this forum post is broken, the following may not be the reason why your code fails, but it is a reason why it doesn’t fully execute the instructions of the exercise. I’m also under the assumption that we’re not dealing with negative exponents.

What happens when you pass in the number 0? We know that the smallest power of two that is greater than zero is 1. However, 2 is returned. How can you fix this?

Hi, Thank you for suggestion, Victoria.
I have modified my code to start with j = 0 in the inner loop and get expected 1 when 0 is passed.

Thank you.
Best regards,
Vadim

1 Like