Unit 6 Lesson 6 Puzzle 16

Can you help me see what I am doing wrong here?

The slider is updating the change in the var speed, but the movement of the motorcycle isn’t picking up the change.

Hi Laura,

The problem is in your updateSpeed function. Instead of updating the speed variable that you already created at the beginning of the program, you have created a new speed variable inside the function. This is because you used var speed = instead of just speed =.

The more confusing thing about the code is that because you created this new speed variable inside your updateSpeed function, that speed variable can only be used inside the function. The other speed variable still exists outside the function, even though its value has not been updated. This concept is referred to as variable scope, and it’s not covered in the curriculum.

So, here is what your code is doing:

  • (line 2) Create a variable called speed and set its value to 7. This variable is accessible everywhere in the program.
  • (line 19) Create a new variable called speed that only works inside the updateSpeed function and set it to the value of the slider. (The original speed variable still exists outside the function)
  • (lines 10/16) Reference the speed variable (the original one that is still 7) to update the position of the motorcycle.

I hope this didn’t go too far down a rabbit hole, but it was a very interesting debugging problem. Please let me know if there’s anything I can follow up on.


Thanks for your reply. If I understand what
you are saying variables are unique to functions. When I drop back to this simple method using an onEvent, why is the new for speed not being shared with the onBoardEvents?

Also why a I getting a warning on line 16?

Thanks, Laura

Hi Laura,

When you moved the variable declaration into the green function block that goes with the slider change event, you made the speed variable local to that function. If you want to be able to access the variable from anywhere in the program, it needs to be a global variable. That means that you should declare it at the beginning of the program, outside of any other blocks.

Once you declare a variable, you should never declare it again, because you will be making a new variable, not working with the one you already have. So, inside all the the functions that use your global variable speed, just update the variable with speed =. Don’t declare it by using one of the var blocks.

I don’t see a warning on line 16, but you may have changed the code since then. If you can tell me the new line or take a screen shot, I can take a look.


I would do that but I can’t see any way to just put a math function inside an onEvent. I have looked at all the documentation and see no examples that provide insight.

Searches of the forums indicated that I should refer to puzzles 12 and 13. That led me down the path of trying to write a function. I am stumped and out of time. Moving on.

Thanks for all of your help,


Hi Laura,

I’m sorry, I wasn’t meaning to talk around the answer. Your original code was almost completely correct, but you just needed to change var speed = to speed = when you updated the speed. If you are working in blocks, you will have to use the block that doesn’t have the var in it, but kind of looks like x=_.

var is only used to create a new variable. You need to use the other block that doesn’t have var in it to update the variable. That one will update the variable, but not create a new one.

It looks like you have something that might work right now, but I can’t tell because I don’t have my board on me, so I can’t run it. I’m also sharing back the original program that you wrote, but I have fixed it so that it works now. All I did was delete the word var from line 21.

1 Like

Hi Elizabeth,
Thanks. Using your input, I finally figured out the puzzle and learned something from this process. I did not understand how variables work in functions.

In the meanwhile, I learned how to use the Debug console. That will be a huge benefit in the class room.


1 Like

Is there an solution example for Puzzle 16? I do not see one.

1 Like