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

everything works one way or another

you could print before and after incrementing to see whether it is having the desired effect and whether that code is run at all

Yes, I expressly stated what adding the increment statement does.

Sorry for saying, “it doesn’t work.”

What I clearly mean, in coder terminology, is that it didn’t evaluate to the desired result. Thanks for all your help!

Better?

Don’t look at the result >.> look at what’s being done. The result is just the last part of what is being done, you’d probably want to look at the first thing instead, see if that’s okay, and then move on to the next.

When I don’t understand what I’m looking at in the first place, to determine whether it’s okay or not, it’s impossible for me to move on to the next.

In other words, I don’t understand the syntax of the code. No matter how many lessons I go through, how much documentation or how many articles I read, how many videos I watch, I do not understand it.

I hope this helps to convey what I’m trying to express. It’s not a matter of testing this or that…it’s a matter of understanding any of it at all. I don’t.

Pick one small piece, figure out whatever you’re missing and then pick the next piece.

The instructions describe what the function should be doing, a start would be to consider what’s involved in that, how would you do that manually, and perhaps you’d write those steps down. You’d now have something to match to the code. You could write comments in the code that say what should happen, and then get the code to match those comments.

The fix isn’t a particular change you’re supposed to magically find. The fix is to observe and adjust. Most things you do in life involve you constantly monitoring them using your senses, and when something’s a bit off you adjust. Same thing here.

I used a LOT of comments in my code, for this very reason. I wish it helped actually. It doesn’t help me understand the code any better.

No matter what I would have done, commented in the code, made written notes about (which I also do), I can tell you with 100% certainty I would have never come up with the solution/fix for this had I not clicked the View Solution button. I struggle constantly with understanding this language - despite having a facility with human languages - no matter how many times I see JavaScript, experiment with it, learn about it (…I should say try to learn), it just doesn’t stick.

I try very hard to work out these projects and lessons. I spend hours on each one, but ultimately I have to look up the answers.

Don’t get me wrong. I’m not giving up, but I’ve never struggled with any subject so much in my life … maybe it’s because I’m “old” who knows?

Looking up answers is probably the problem. Exercises exist for you to traverse them, to find the path to the solution, not the solution itself. Whenever you look up an answer you’re avoiding the thing you needed. Stick around instead, the point isn’t to be done.

1 Like

At a certain point, I have to eat and sleep and, believe it or not, earn a living for me and my family. If I could spend all day on solving intermediate JavaScript challenges on Codecademy, I would. I do my best with the time I have, but I’m human. I spend untold hours reading documentation – which I don’t understand. I come to these boards and ask questions to get help – the responses for which I do not understand. I’m here on a scholarship, so yes, while I understand the point isn’t to be done, eventually I do have to move on.

Looking up answers is probably the problem.

As mentioned, this is a solution of last resort, so while I appreciate your pointing out my problem, it’s not getting me any closer to understanding this topic.

Thanks for all your help.

The starter code.

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
            i = 1;
            while (i < number) {
                  i = i * 2;
            }
            results.push(i);
      }
      return results
}

It’s not doing what we want. Can you fix our code, please?

Can we reason what the outcomes should be? When we run the code with the given inputs, is that the outcome we get? Short pause… No. We get,

[8]

Let’s see if we are in fact at least working with the first element of the list.

numbers.unshift(15)
smallestPowerOfTwo(numbers)

[16]

Good. That means the math is behaving as expected, but we should have a list of five values, here, not just the first result. Granted we do have a list, which is also a good sign.

The indication here is that the numbers list is not being iterated properly. Where does that take place?

1 Like

I’m not saying anything about how much you should do, that’s none of my business.

I’m saying you want to be stuck at the simplest possible thing you can be stuck at. Being stuck means having found something to learn. Good. Set up a tent, build a campfire, hang out there until you’re through.

If you understand none of what is involved, then that’s just saying you skipped things.

So you’d learn small things, like, here’s how to create a variable, here’s how to add numbers, here’s how to loop, here’s how to print.

When you know a couple of small things you can use them together to build something bigger. So you’d build a couple of bigger things. Now you know those. Then you can build things out of those, as well.

Learn the basic building blocks until they become things you’re certain of. Then you combine them. That way you move between certainties, always standing on firm ground and ready to process some single new thing in the context of what you already know.

A big part of this is expectations. Don’t expect to be able to use something you don’t know. Demand to know every single thing you’re using, observe every action carried out.

1 Like

There are two loops, so one of the loops isn’t working as expected … or more rather, as desired.

Questions:

  • How does one determine whether to use two loops? (And for me, why?)

  • Then how do you figure out which one isn’t doing what you want it to?

  • Lastly, how do you figure out how to make it do what you want?

That’s the plan. That’s always been the plan. I simply struggle with it, with the knowing…I think I know it, then I’m certain I do, I practice it, but then when these coding challenges came up, it’s like it was all gone. Poof. I was fooling myself, thinking I understood it before. I get so close on these solutions but then close isn’t good enough. Coding doesn’t reward close. I’ve never worked with such exacting information. I guess I just get frustrated. Apologies.

If you look at the input there are several values to process in turn. So you’d use a loop.
At some other point you’re looking to test different powers of 2. So you’d loop through them.

By the time you’ve considered what should happen you’ve already decided on the number of loops. That’s not related to code. If you write instructions for another person to carry out, you would have two loops in that also. It’s also a bit akin to deciding how many times you’d use the letter “a”. You’d use it however many times you need to to describe what you want, you don’t pick a particular number.

By observing and comparing to what should happen. If you watch somebody else do it, and they do it wrong, would you notice? No different.
Alternatively you can read what it says and consider whether that’s saying the right thing. That would be a more human, soft way of reasoning about it that isn’t easy to describe and isn’t as foolproof. But you’ve probably been doing it all your life so it isn’t new. If this doesn’t immediately give results then start making observations instead since that is the more methodical approach.

Again not related to code. You decide that first. Then you describe those things with code.

2 Likes

Something about the inner loop is interfering with control of the outer loop. Can you zero in on that something?

The variable “i” cannot be referenced in both the inner and outer loops without it causing problems. I think this is where the error is. The “i” needs to be changed to something else in the inner loop for the code to work as desired.

2 Likes

Now you’re cooking with gas.

1 Like

Well that took long enough.

Be patient. There is no clock on this. Keep cycling back, even to the very beginning and play with the code concepts and patterns. Learn to expect a result and get that result. Keep the bar low until you can really climb up to the next concept level. Repeat, cycle back regularly.

Especially expect a lot of mistakes. It’s why programming languages provide the level of support that they do. We make a lot of mistakes and the interpreter usually gives us enough information to track them down when they break our program.

Mistakes that mess up our data are harder to find. They don’t cause (throw) any runtime errors. Can we trust the data coming out the other end? That’s anyone’s guess. This lesson is a perfect example of that scenario. The code ran, but didn’t give us the result we expected.

1 Like

???

Where is the support your reference for programming languages? Are you referring to Codecademy? I don’t think I understand.

No, I’m referring to the error messages they keep in store to help us trace back to the error and what caused it.
Statement

console.log(a)

Error message

console.log(a)
            ^

ReferenceError: a is not defined