Categories: web development

How to create a simple calculator program with javascript

This is a tutorial about how to create a simple calculator program with javascript, css, and html.

First create index.html file

<html>
<head>
    <title>Dice Simulator 2015</title>
    <link rel="stylesheet" href="style.css">
</head>  
<body>
  <h1>Calculator Example</h1>
  <p>Open the JavaScript developer console to interact.</p>
  <script src="calculator.js"></script>

</body>
</html>


Then create style.css file

* {
  font-family: Helvetica, Arial, sans-serif;
  text-align: center;
}


create calculator.js file

var calculator = {
  sum: 0,
  add: function(value) {
    this.sum += value;
  },
  subtract: function(value) {
  this.sum = this.sum - value;
  },
  multiply: function(value) {
  this.sum = this.sum * value;
  },
  divide: function(value) {
  this.sum = this.sum / value;
  },
  clear: function() {
    this.sum = 0;
  }, 
  equals: function() {
    return this.sum;
  }
}


After open javascript console
Type in commands example
calculator.add(1)
calculator.add(1)
calculator.equals() // 3

Admin

Share
Published by
Admin

Recent Posts

Unveiling the Intricacies of the Tinder Algorithm: 5 Surprising Metrics

Monitored by Tinder The inner workings of the Tinder algorithm remain shrouded in secrecy, known…

3 weeks ago

Is Shopping Temu Safe

Use fav19073 30% OFF Shop Now I recently had a fantastic buying experience on Temu,…

4 months ago

Cheap Netflix Subscription

Unlocking Affordable Streaming: How Gamsgo.com is Revolutionizing Netflix Subscriptions Sign Up Save1 coupon code for…

4 months ago

Domains for SALE.

razerkeyb.com gloriousmouse.com

9 months ago

How to Start your own vegetable garden?

Planting your own vegetables and fruit is a great way to save money and eat…

1 year ago

Jobs AI can’t replace

While AI has the potential to automate many tasks, there are still some jobs that…

1 year ago