- Profit: (SP) – (CP)
- Profit Percentage: Profit/CP x 100
- Loss: (SP) – (CP)
- Loss Percentage: Loss/CP x 100
- In the body tag, create the design and layout of the calculator using basic HTML.
- Use CSS properties for stylings such as alignment, size, background, etc.
Example: We will use the above approach to create a calculator.
Explanation: A Calculate() function will be invoked when the user enters the CP and SP amount in the input and clicks the Calculate button using the onclick event attribute. In this function, we have used DOM querySelector() method to select the value entered in the inputs into a variable using their class names. If the SP amount is greater than the CP amount then Calculate() function will calculate the profit and profit percentage otherwise calculate the loss and loss percentage using the above-mentioned formulae and displayed the text using the innerHTML property.