Help a student of mine debug a u7 project

Link to the project or level: App Lab - Code.org
What I expect to happen: the lives counter go down by 1 even when you restart the game

What actually happens: after restarting the game it goes down by 2,

What I’ve tried: I added a watcher and the display and variable are the same, I ran it slow and then saw that once you llose and go back to the start screen and play again incorrectAnswerSelection(questionNumber); ends up running twice for whatever reason… I believe it must be an error with that onEvent is inside the function but I am not sure

Hi @yanet.cabrera ,

Thank you for writing in. In accordance with the College Board’s guidelines for AP CS Principles Exam, we limit the debugging assistance we provide from March through the submission deadline for the Create Task.

From page 14 of the CB’s student handouts:

“you may not seek assistance in writing, revising, amending, or correcting your work, including debugging the program, writing or designing functionality in the program, testing the program, or making revisions to the program, from anyone other than your collaborative partner(s). "

Please verify this project is not part of a student’s Create Task for AP CS Principles.

If it is, we can only verify whether there is a technical issue with the platform causing the bug.
If it is not, we are happy to help locate the bug and provide guidance on fixing it!

Thank you,
Michael K.

Hi! Correct, it is not for the create tak. Its a program he was working on Unit 7, I told him to turn it in but I still havent had a chance to try to help him debug it, error only happens after the first time playing the game. If you could try to find his bug, I’m sure he’d really appreciate it!

It looks like setAnswerChoices() is called each time through the game. That means each time through the game you will add another onEvent() callback to each button. All of the callbacks added to a button are called when the button is clicked. So on the second time through you will get 2 wrong answers, 1 right and 1 wrong, or 2 right answers. On the third time through you will get 3 wrong answers, 1 right and 2 wrong, etc.

Only add callbacks once.

1 Like

Do you mean take the onEvents outside of the setAnswerChoices function? I don’t really understand the fix or why one the first time the onevent and then future times it runs it multiple times

You buy a puppy. You train that puppy to run to the kitchen and get you a towel. You get a second puppy. You train that puppy to run to the kitchen and get you a towel too.

You yell out “get me a towel”. How many towels are you going to get?

Each time you call onEvent() you buy another puppy. So if setAnswerChoices() calls onEvent() then the second time you call setAnswerChoices() how many puppies do you have? How many towels will be getting?

Calling onEvent() inside another function is next level complex. So don’t do that.

1 Like

I get it in theory I just don’t understand why it would call the onEvent twice… but I guess ill just tell him what I already told him which is that using onEvents inside functions will cause some issues