JavaScript Calculator Example

In this tutorial, I’ll tell you 2 ways in which to create a

calculator exploitation basic markup language, CSS and JavaScript. In 1st

means we tend to make performs for all operations and within the second we use some integral international function to create a calculator.

In each case we tend to are aiming to add some basic math operations like addition, subtraction, multiplication,

division in our basic calculator. This project can assist you to enhance your logical ability and secret writing skills.

JavaScript Calculator Example

Method 1:
Mainly the functions that we’d like to figure for calculator are handling the keypresses,

inputting the digits, inputting the decimal point/key, handling operators, resisting the calculators,

change the show, equal key and clear key.

As we all know calculator consists of keys and show. So, within the UI perspective we’ve got to figure on:

calculator tile – wherever we tend to show the title

input buttons – wherever we tend to show all keys

output screen – this is often the screen wherever we are able to check what input

we tend to are coming into and showing all the values that we entered.

Before beginning this Project you need to have data or plan of those things:

if-else statements
javascript functions
arrow perform
some operators like && and ||
change the text with the worth attribute
add event listeners
string ways like includes contain etc.
eval perform
So, secret writing half begins here for the primary case. Initially, we’ve got to create a div instrumentality during which we tend to are aiming to add all buttons with correct styling. to create this UI we tend to use CSS Grid box that is that the best observe of writing correct responsive code. After this, we tend to begin work on functionalities.

Firstly we’ve got to initialise the worth of every variable and constant.

Then we tend to check what secret is ironed by the user like operators, operands,

decimal or clear consistent with this key we tend to store it in numerous variables that we declared. So, once the user hits variety keys

then we tend to store these values in operands. we tend to outline the separate

perform for every operation like inputting the digit, change the show, decimal enter etc.

You can check the entire code below.

    <input type="text" class="output-screen" value="" disabled />

    <div class="display-keys">

      <button  class="operator" value="+">+</button>
      <button  class="operator" value="-">-</button>
      <button  class="operator" value="*">×</button>
      <button  class="operator" value="/">÷</button>

      <button  value="7">7</button>
      <button  value="8">8</button>
      <button  value="9">9</button>

      <button  value="4">4</button>
      <button  value="5">5</button>
      <button  value="6">6</button>

      <button  value="1">1</button>
      <button  value="2">2</button>
      <button  value="3">3</button>

      <button  value="0">0</button>
      <button  class="decimal" value=".">.</button>
      <button  class="clear-value" value="clear-value">AC</button>

      <button  class="calculate-value operator" value="=">=</button>

JavaScript Calculator Example

Method 2:

We can conjointly create JavaScript calculator exploitation the eval perform.

This perform is employed to gauge any string. this is often an inherent perform of JS.

Here we tend to used the table tag to create UI and for adding further

functions we use form markup language component. On every keypress, we tend to placed on click technique to feature

that price to the output screen. And once this eval perform judge all values once you click on any operator.

<form name="calculator">
        <td colspan="4">
           <input type="text" name="display" id="display" disabled>
           <td><input type="button" name="one" value="1" onclick="calculator.display.value += '1'"></td>
           <td><input type="button" name="two" value="2" onclick="calculator.display.value += '2'"></td>
           <td><input type="button" name="three" value="3" onclick="calculator.display.value += '3'"></td>
           <td><input type="button" class="operator" name="plus" value="+" onclick="calculator.display.value += '+'"></td>
           <td><input type="button" name="four" value="4" onclick="calculator.display.value += '4'"></td>
           <td><input type="button" name="five" value="5" onclick="calculator.display.value += '5'"></td>
           <td><input type="button" name="six" value="6" onclick="calculator.display.value += '6'"></td>
           <td><input type="button" class="operator" name="minus" value="-" onclick="calculator.display.value += '-'"></td>
           <td><input type="button" name="seven" value="7" onclick="calculator.display.value += '7'"></td>
           <td><input type="button" name="eight" value="8" onclick="calculator.display.value += '8'"></td>
           <td><input type="button" name="nine" value="9" onclick="calculator.display.value += '9'"></td>
           <td><input type="button" class="operator" name="times" value="x" onclick="calculator.display.value += '*'"></td>
           <td><input type="button" name="zero" value="0" onclick="calculator.display.value += '0'"></td>
           <td><input type="button" name="doit" value="=" onclick="calculator.display.value = eval(calculator.display.value)"></td>
        <td><input type="button" class="operator" name="div" value="." onclick="calculator.display.value += '.'"></td>
      <td><input type="button" class="operator" name="div" value="/" onclick="calculator.display.value += '/'"></td>
              <input type="button" id="clear" name="clear" value="clear"  style="width:12%" onclick="calculator.display.value = ''">
JavaScript Calculator Example

Leave a Comment

error: Alert: Content is protected!!