close

DEV Community

Mohamed Haarish
Mohamed Haarish

Posted on

simple javascript calculator

this code is used to create a simple calculator using javascript.

1.three important variables

let previousvalues ="";
let operator = "";
let currentvalue ="";

. Previous Value → first number
. Operator → +, -, *, /
. Current Value → second number

2.ENTERING NUMBER

appendnumber() adds the number we press.

for example;

1 → 2 → 3

plain text
currentvalue =123

3.selection an operator

When we press +, -, *, or /, the calculator stores the operator.
example;

plain text

10+5

now;

plain text

This JavaScript code is used to create a simple calculator.

  1. Three Variables

let previousValue = "";
let operator = "";
let currentValue = "";

. Previous Value → first number
. Operator → +, -, *, /
. Current Value → second number

  1. Entering Numbers appendNumber() adds the number we press.

For example:

1 → 2 → 3
currentValue = 123

  1. Selecting an Operator When we press +, -, *, or /, the calculator stores the operator. Example: 10 + 5

Now:
the calculator uses three main values ;
previousValue = 10
operator = +
currentValue = 5

  1. Calculating The calculate() function performs the operation.

10 + 5 = 15
10 - 5 = 5
10 * 5 = 50
10 / 5 = 2
Number() converts the value into a number before calculating.

Easy to Remember

Previous Value = First number
Operator = What operation to do
Current Value = Second number
Calculate = Gives the answer

So the calculator simply works like:

First number → Operator → Second number → Answer

Top comments (0)