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

This community-built FAQ covers the “Fix The Broken Code!” exercise from the lesson “Code Challenges: Intermediate JavaScript”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise Fix The Broken Code!

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I’m confused with instructions regarding this exercise… what exactly are we suppose to do? what’s problem we are asked to solve? wording is terrible.


I can see what’s wrong but still don’t understand what the function should do.

the smallest power of two which is greater than the current element

What does this mean? So 8 is the smallest power of 2 which is greater than the current element 5? Please explain.


The wording is fine… Try identifying what you don’t understand, for example, what is a power of two, which such values exist, and which of them is the smallest one that is greater than some other value, and which value is that other value?


Thanks guys, i figured out but just wan’t sure what was being asked…

The wording truly is terrible. How about rephrasing it: "We loop through the argument array and push in the smallest number which is greater than the current element, which consists of two multiplied by an integer.

Took me 10-15 minutes to get what was asked.

1 Like

Cool. Now, more than an hour after my last post I truly realised what I have to do. Ignore my last post, turns out I still didn’t understand what I was supposed to do, so the wording I suggested was wrong.

So the original problem of instructions unclear remains. And my brainpower is too exhausted to offer alternative wording.

but basically left column is the original numbers and right column is what is supposed to be returned. The forum doesn’t allow multiplication symbol, so instead I wrote “times”.

5 < 8 (1 times 2 times 2 times 2)
3 < 4 (1 times 2 times 2)
9 < 16 (1 times 2 times 2 times 2 times 2)
30 < 32 (1 times 2 times 2 times 2 times 2 times 2)

The challenge does a good job of walking us through the use of outer and inner loops, but the author may not have considered how well learners understand exponents or powers of 2. Even without that knowledge, the loop method works well if we keep successively multiplying by 2 until the value is greater than the given number.

Just be glad the author didn’t expect us to understand logarithms. (This is not a solution, since we are expected to repair the existing code.)

const log2 = Math.log2;
const ceil = Math.ceil;
const numbers = [5, 3, 9, 30];
const smallestPowerOfTwo = arr => {
  let results = [];
  arr.forEach(x => results.push(2 ** ceil(log2(x))));
  return results;
// <- [ 8, 4, 16, 32 ]


 > const smallestPowOfTwo = arr => {
     return arr.map(x => 2 ** ceil(log2(x)));
<- undefined
 > smallestPowOfTwo([5, 3, 9, 30])
<- > (4) [8, 4, 16, 32]

Pay no heed if you haven’t touched upon iterators, yet. Keep leaning how to use for.

I am on the same page, it suggests for the ‘smallestPowerOfTwo’ of the array, which would be the power of 2 of the ‘numbers’ and then pointing out the smallest of them all. However the suggested answer states the answer: [ 8, 4, 16, 32 ], now how in the world is that in any way a power of two related for the initial numbers [5, 3, 9, 30]?

I am lost… anyhow I managed to create an array with the power of two of the initial array and I was getting ready to single out the smaller… but then… it doesn’t seem what the exercise is about:

//Getting the power of 2 of an array of numbers.

const powerOfTwo = arr =>{
  let resultsTwo = [];
  for (let i = 0; i < arr.length; i++){
  return resultsTwo;
1 Like

There is no relation between the two lists except that the second list corresponds to the nearest power of two that is greater than the original number.

8 > 5
4 > 3
16 > 9
32 > 30
1 Like

why don’t you need to declare the variable number ?

1 Like

Who made this challenge because it’s worded poorly and I’m not sure what you’re suppose to do…

Yes! I have the same question.

A question that cannot be easily answered given the information and no example to follow, mind. Please elaborate and give us a code example to help us understand the question.