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


#1

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!


#2

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.


#3

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

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?


#5

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


#6

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.


#7

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)


#8

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.