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

Except that there’s stackoverflow, Github and a million other helpful communities that provide similar support to real-world developers, but yeah, I definitely get your drift.

FYI, I’m not planning to be a “coder” per se - I’m going for more web and UX/UI design. That said I believe it’s invaluable and important to learn as much about HTML, CSS and JavaScript as I can, so I definitely see the value in hanging in there when frustration and resistance bear their ugly little heads.

One of my favorite quotes by Lillian Hellman is: “Things start out as hopes and end up as habits.”

And when I inevitably need more motivation when I’m feeling frustrated, I turn to Zero-to-Programmer, @zero2programmer.

In fact, I find myself returning to this blog post again and again…it keeps me going more days than I care to admit >>>

The Inner Game of Self-taught Development: Consistency vs Passion https://zero2programmer.substack.com/p/the-inner-game-of-self-taught-development?utm_medium=email&utm_campaign=cta


Yep, I finally figured it out. Thanks!

that page!! Thanks for that! I’ve just scanned through and it look like a bible for coder! :joy: :joy:

1 Like

BTW, did my hint helped or you’ve managed to figure that out before? JW

1 Like

Honestly this is the best example of how to understand what is going on in this practice problem.

It’s important that everyone know that inner loop will run until the condition is true . Honestly none of us have seen an example like this through out the course and some of the projects go from level 2 to 8.

Everyone remember computer are dumb and we are telling them what to do. So its imperative we not focus on syntax but the principals and overall logic of the program.

Hang in there with programming you have to fail your way to success. There’s been a few projects I couldn’t understand even with the solution being presented. Keep drudging a long. You got this and don’t give up!!!


This Challenge took a lot of times trying to figure out what should I do with it ,
after many tries I figure out that there is two type of loop :slight_smile:
first one the outer loop (for loop) that go throw an array given to us :
as numbers = [5,3,9,30]
so the outer loop is to go throw the array like this :
for (let i = o; i < array.length; i++){
// inside it we define the number and assign it to array[i];

here where we need to focus now While loop as the inner loop that we will try to understand it
first mistake is that we assign i variable to 1 ??? because i related to the outer loop so it will effect it but how ???
after i variable go throw while loop we change the value of i to 8 as our first results so i will stop our outer loop because the array length is smaller than i so the condition will be false and it will stop>>
lets take it step by step
first we will not use any variable here just to explain what going on ,
we will start with 5 first digit from numbers array
and remember that in the broken code we assigned i to 1 so lets go on:
while 1< 5 is true multiple 1 with 2
while 2< 5 is true multiple 2 with 2
while 4< 5 is true multiple 4 with 2
while 8< 5 is false push i variable to results array .

and here after we assigned the i variable to 8 the condition for outer loop will be false like this
for (let i = 0; 8 < array.length // here it is equal to 4 so the condition is false and the results array will contain just one digit and it will be 8 ,

I’m not trying to give you the salutation guys because you need to learn by doing I just explain the problem so it will be easy to be fixed by you > I hope I explain it clearly good luck to everyone > :slight_smile:

After 2 days of blank stare on my laptop screen, I finally got the answer!!! I cant believe its a very very very simple fix!!!

Thanks for this mate, I found the fix to be very simple for this problem but the logic behind the while threw me for a loop… pun intended. :slight_smile:

Just a little of explanation for to see if I’m understanding what we’re trying to get the code to do here. Took me a while to see what’s going on.
We want the first for loop to iterate through each value in the numbers array and each time it does the second loop needs to find a power of two that is larger than each value.
So for each iteration we need to generate a sequence of powers of two (2^1, 2^2, 2^3, 2^4…) and see if it’s bigger than the value we’re looking at in the numbers array. If it’s smaller we move on to the next power of two in the sequence (by putting the too-small back in and multiplying it by two again). If it’s bigger then we exit the loop and return that value into a new array.
So for each iteration the code is doing this ( where ‘j’ is the power of two):

number = 5
j= 2 (smaller, chuck it back in)
j= 4 (smaller, chuck it back in)
j= 8 (bigger! Put in in the new array)
number = 3
j= 2 (smaller, chuck it back in)
j= 4 (bigger! Put in in the new array)
number = 9
j= 2 (smaller, chuck it back in)
j= 4 (smaller, chuck it back in)
j= 8 (smaller, chuck it back in)
j= 16 (bigger! Put in in the new array)
number = 30
j= 2 (smaller, chuck it back in) (1 * 2)
j= 4 (smaller, chuck it back in) (2 * 2)
j= 8 (smaller, chuck it back in) (4 * 2)
j= 16 (smaller, chuck it back in) (8 * 2)
j= 32 (bigger! Put in in the new array) (16 * 2)
[ 8, 4, 16, 32 ]


Hello @andycatlow, and welcome to the forums!

Yes, I believe you’ve summed up the exercise fairly well.

Hi all.
I’m stuck on this one. I’m getting correct console output [ 8, 4, 16, 32 ], but I’m still being failed and getting a message stating: Your function should work on any array of numbers, not just the numbers array.
I figured that the original solution wouldn’t return correct value if original array contained 0 or 1, so I added an extra if statement that deals with such scenario. Still, I’m being failed. Why?


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

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

            if (number < 2) {
            } else {
              let j = 2;
              while (j <= number) {
                j = j * 2;
      return results;

It’s weird. I started again. I declared ‘numbers’, swapped ‘i’ for ‘j’, and it now passes, even though the function no longer works correctly, if you add 1 or 0 to the ‘numbers’ array. Is it me or is this exercise not entirely correct? :roll_eyes:

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

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

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

Only thing I would change in the above is how we read the array. Since we are not altering anything we can read by value, rather than index reference.

for (let number of arr) {


Now you have the number variable and the rest stays the same, save perhaps one minor tweak,

   j *= 2
1 Like

I see. I thought for...of loop (much like for...in) is meant to be used with objects only - not arrays. If you can use it with arrays as well, then that’s quite a neat trick given that you don’t need access to index. Still I’m not sure if it answers my question. Why was the first version of code failed while the second passed, if both produce correct output for the given array example? Not to mention that the top version produces correct results for all numbers - not just ones above 2.

for...of    =>  array element value scan

for...in    =>  object key scan

Could have something to do with the reason why it doesn’t pass. As positive exponents go,

2 ** 0  =>  1
2 ** 1  =>  2

Re: power of two exponents


Of course… back to school it is…

Thanks for the for..of tip!

1 Like

I’m curious why you used the fat arrow in this statement? In the programming language referenced (JS) it’s not equivalent to =, so I’m confused about this syntax. Thanks for explaining.

It’s not code, but narrative. In that context it points to the resulting evaluation.

What is the easiest way to step through the code in order to find the error that is occurring? I vaguely recall that Chrome allows us to do something like this; or would you simply use console.logs in practice?

Found the answer! cant believe it was so simple FML! lol

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

const smallestPowerOfTwo = arr => {

  let results = [];

  // The 'outer' for loop - loops through each element in the array

  for (let j = 0; j < arr.length; j++) {

        number = arr[j];

        // The 'inner' while loop - searches for smallest power of 2 greater than the given number

        i = 1;

        while (i < number) {

              i = i * 2;




  return results



// Should print the returned array [ 8, 4, 16, 32 ] instead prints the returned array [8]

I feel ya! I’m having the same trouble: it just doesn’t seem to stick no matter what I try/do. I’ve gone 125 days in a row and still the bank is empty! :grimacing: