Activity 6
Correct Answers
1. How is a method different from a function? ✅ A method is attached to a class or object.
2. What does the every() array method do? ✅ Checks if all elements pass a condition.
3. Which method is used to check if a string starts with a specific substring? ✅ startsWith()
4. How can you replace all occurrences of a substring? ✅ Use the replaceAll() method.
5. Which method creates a date object from milliseconds? ✅ Date(milliseconds)
6. What does the sqrt() method do? ✅ Returns the square root of a number.
7. What is a major risk of using eval()? ✅ Security vulnerabilities like code injection.
8. What is the purpose of the concat() string method? ✅ Combines two or more strings.
9. What is a built-in JavaScript method? ✅ A predefined function provided by JavaScript.
10. How can you format a number to two decimal places? ✅ Use the toFixed(2) method.
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 .html extension (e.g., Activity 6.html).
Create a Simple Calculator Interface
- Add two input fields for users to enter the first and second numbers.
- Include buttons for basic arithmetic operations:
- Addition
- Subtraction
- Multiplication
- Division
- Remainder
- Clear
Implement the Functionality
- Use JavaScript to retrieve values from the input fields.
- Perform the selected operation when the corresponding button is clicked.
- Display the result in an alert box.
- Ensure error handling for cases such as:
- Empty input fields.
- Division by zero.
Final Output
- Your calculator should allow users to:
- ✅ Enter two numbers.
- ✅ Perform basic arithmetic operations.
- ✅ Receive alerts displaying the results.
- ✅ Clear the input fields when needed.