# 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:

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

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

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.

12 Likes

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.

4 Likes

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?

2 Likes

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;
}
console.log(smallestPowerOfTwo(numbers));
// <- [ 8, 4, 16, 32 ]
``````

or,

`````` > 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++){
resultsTwo.push(arr[i]*arr[i]);
};
return resultsTwo;
};
console.log(powerOfTwo(numbers));
``````
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 ?

2 Likes

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

1 Like

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.

2 posts were split to a new topic: Question should be modified

Could I have used for loop instead of the while loop here?

``````       const newNum=0;
for (let j=1; j< number; j*=2)
{newNum.push(j)}
return newNum;
``````

Did you try it? Not sure how return fits in since we cannot see your complete code.

I tried it didn’t work, but i’m trying to understand why it didn’t work. Here is my full code:

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

const smallestPowerOfTwo = arr => {
let results = [];

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

const newNum=0;
for (let j=1; j< number; j*=2)
{newNum.push(j)}
return newNum;
return results
}

console.log(smallestPowerOfTwo(numbers));
``````

It looks like you are treating the for loop (inner) like a `forEach()`. It’s not, and does not behave the same way.

Does not belong.

Should be accumulating. It is not an array.

``````    for (...) {
newNum = j
}
results.push(newNum)  // in the outer loop
}    // be sure to close outer loop before return.
return results
``````

Also, `newNum` should be declared with `let` so it can change in the loop.

Once you get it working check the result. See anything wrong?

``````[4, 2, 8, 16]
``````

There is a common thread that suggests one final step is needed but we cannot configure the loop for that, so must do it on `newNum` before we push it to the array.

``````results.push(newNum * 2)
``````

Like so,

``````smallestPowerOfTwo = arr => {
const results = [];
for (let i = 0; i < arr.length; i++) {
number = arr[i];
let newNum;
for (let j = 1; j < number; j *= 2) {
newNum = j;
}
results.push(newNum * 2);
}
return results
}
``````
2 Likes

Super lost here. All I can see/identify is that the inner “while” loop doesn’t include the increment statement, but if I add one, it still doesn’t work. All that does is turn the currently returned array of `[8]` into a returned array of `[10]`.

I’m sure the ‘fix’ for this code is simple, but I’m not simple enough to figure it out.

JavaScript (or rather, my lack of the understanding of it) is seriously on the verge of sending me over the edge. I desperately want to understand this stuff, and I just…don’t. Am I the only one here that is this miserably lost?

Sorry…venting.

Ok, so can someone please point me to a possible solution? I’ve looked at the Hint, and it did nothing to give me a better understanding of what I’m supposed to do/fix.

1 Like