Simple Calculator
Medium
16
37.2% Acceptance
Welcome to the React Calculator Lab! In this lab, your task is to create a basic calculator that performs addition, subtraction, and multiplication. This calculator will provide instant feedback on the operations performed.
Instructions
- Create Input Fields:
- Design two input fields of type
number
. - The first input should have an id of
num1
and a default value of0
. - The second input should have an id of
num2
and a default value of0
.
- Design two input fields of type
- Display Output:
- Have a paragraph tag with the id
output
where the result of the operations will be shown.
- Have a paragraph tag with the id
- Perform Operations:
- Implement a button with the id
add
. When clicked, it should calculate the sum ofnum1
andnum2
and display the result in theoutput
paragraph tag. - Similarly, include buttons with ids
subtract
andmultiply
. Thesubtract
button should compute the difference, while themultiply
button should compute the product ofnum1
andnum2
.
- Implement a button with the id
- Reset Functionality:
- Introduce a button with the id
reset
. Upon clicking, theoutput
should be cleared, and bothnum1
andnum2
should revert to their default values (0
).
- Introduce a button with the id
Tips
- This lab focuses on DOM manipulation using React. Remember to update the state properly whenever any user interaction occurs.
- Use React's controlled component pattern to link the input fields with the state.
- Ensure you handle events correctly. Each button should have its respective event handler to perform the desired operation.
- Test your application thoroughly after each step to ensure that all functionalities are working as expected.