Module 3: Operators and User Interaction
Lab: Simple Calculator with Dialogs
Build a simple calculator that asks the user for two numbers and an operation, then displays the result using alert()
.
Tasks to Complete:
- Use `prompt()` to ask the user for the first number. Convert the input to a `Number`.
- Use `prompt()` to ask the user for the second number. Convert the input to a `Number`.
- Use `prompt()` to ask the user for an operation (e.g., "+", "-", "*", "/").
- Use `if`, `else if`, and `else` statements (or a `switch` statement) to check the operation entered.
- Based on the operation, perform the corresponding arithmetic calculation.
- If the operation is division, check if the second number is zero. If it is, `alert()` an error message like "Cannot divide by zero!".
- If the operation is valid and calculable, `alert()` the result in a user-friendly format (e.g., "Result: 5 + 3 = 8").
- If an invalid operation symbol is entered, `alert()` a message like "Invalid operation.".
JavaScript Sandbox
Output will appear here...
JavaScript execution is sandboxed. Output from `console.log()` and errors will appear above.