Read & Predict
Practice reading JavaScript code, predicting its output, and verifying your understanding before running the program.
#teaching #activity #javascript #functions #if-statement #debugging
One of the most important programming skills is being able to read code before writing code. Professional developers spend much more time reading code than typing it.
In this activity, you will analyze a JavaScript function, predict what it does, and then verify whether your prediction was correct.
Learning Objectives
By the end of this activity, you should be able to:
- Read a JavaScript function from top to bottom.
- Identify the purpose of an
ifstatement. - Follow the flow of execution.
- Predict a function's return value.
- Verify your prediction by running the code.
The Big Idea
Only after making a prediction should you execute the program to check your understanding."What do I think this code will do?"
Code to Analyze
function calculateTotal(bill, tipPercent) {
if (bill < 0) {
return "Invalid bill";
}
let tip = bill * (tipPercent / 100);
return bill + tip;
}
calculateTotal(100, 10);
Instructions
Read the Code
As a group, read the function from top to bottom.
Do not run it yet.
Make Your Predictions
Write your answers in your activity log before checking the result.
Answer the questions below.
Verify Your Answers
Paste the code into your editor or use an AI assistant to execute it.
Compare the actual output with your prediction.
If you were wrong, explain why.
Questions
Answer the following before running the program.
- What value does
calculateTotal(100, 10)return? - What happens if
billis-5? - Which line prevents invalid bills?
- Identify:
- the function
- the
ifstatement - the variable
- the mathematical calculation
Reflection
After verifying your answers, write a short reflection.
- Were your predictions correct?
- What surprised you?
- What did you learn about how JavaScript functions execute?
Expected Output
By the end of the activity, you should have:
- A completed prediction log.
- Verified answers after running the code.
- A short reflection explaining what you learned.
Submission
Submit a document containing:
- Your answers to the four questions.
- The actual output after running the program.
- Your reflection describing whether your prediction was correct.
Grading Rubric
| Category | Points |
|---|---|
| Correct Predictions | 30 |
| Code Identification | 25 |
| Reflection & Explanation | 25 |
| Participation & Completion | 20 |
| Total | 100 |
Exercises
Practice problems that build skill through repetition.
AI Prompts
Prompt templates for teaching with and about AI.