I have students working in App Lab. They created a quiz and would like to display the score at the end of the quiz. We know their score is updating correctly (we’ve coded it to display the score in the console log), but we can’t figure out how to get the score to display on their last screen.
Basically, the score is a variable and we want to output the value of the variable on the last screen. We know it must not be difficult, it’s just some syntax we’re missing!
Create a text label and set the text of the label to the score.
Yes! That’s what we are trying to do! What is the code for that? Or is it done in design mode (if so, how?)
Here’s a program that takes input from a slider and converts a Fahrenheit temperature to a Celsius one and then displays the output in a text label. This could help you see an example.
You can use design mode to create the label. Then, there’s two options:
You somehow need to make the score update every time it changes. I can’t tell if you can do that or not, but most likely. I could tell you more information if you’d share me the link to the app. To update the score on the screen you need to use the setText() function.
If you don’t, or you’re having trouble doing this method, then do this instead. Note that this may be slower, and may cause performance errors in the future. It’s also not a very good practice in programming, but here it is:
Use setInterval() to create an interval to update the score. You can choose which rate you want to update it with, but you probably want to set it to the lowest you can, so 1. To update the label, you need to use setText() just as in the previous method. Tell me if this works!