Activity 5
Correct Answers
1. What is a real-world example of using functions? ✅ Validating form inputs in HTML.
2. How do you integrate functions with HTML? ✅ Use HTML events like onLoad.
3. How can you call a function in JavaScript? ✅ By using its name followed by parentheses.
4. How do you define a basic JavaScript function? ✅ Using the function keyword.
5. What happens if no return value is specified? ✅ The function returns undefined.
6. What is required when defining a function? ✅ A name, parentheses, and a code block.
7. What determines if a variable is local or global? ✅ The place where it is declared.
8. What is the main purpose of a function in JavaScript? ✅ To organize code into reusable segments.
9. How do you handle errors in a function call? ✅ Use conditional checks before calling functions.
10. What is an advantage of modular functions? ✅ They make debugging easier.
Instructions:
Set Up Your Development Environment
- Open your preferred code editor (e.g., VS Code, Sublime Text, or Notepad++).
- Create a new file and save it with the
.htmlextension (e.g., Activity3.html).
Change Background Color Using JavaScript
- Create multiple buttons, each assigned to a different color.
- Use JavaScript functions to change the background color when a button is clicked.
- Include a button to reset the background to white (default color).
- Use
document.body.style.backgroundColorto apply the color change.
Final Output
- ✅ A webpage with buttons that change the background color.
- ✅ Clicking each button should instantly change the background to the selected color.
- ✅ A Reset button should restore the background to white.