Skip to content

Instantly share code, notes, and snippets.

@eimihar
Created November 23, 2017 06:08
Show Gist options
  • Save eimihar/84f35b96ff9880885f4605de76b67225 to your computer and use it in GitHub Desktop.
Save eimihar/84f35b96ff9880885f4605de76b67225 to your computer and use it in GitHub Desktop.
20 minutes arithmetic calculator
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<style type="text/css">
body {
background: #c3c3c3;
}
.body .buttons {
float: left;
width: 20%;
height: 50px;
text-align: center;
padding-top: 20px;
font-size: 25px;
cursor: pointer;
}
.result {
height: 50px;
background: #efefef;
padding: 10px;
}
.calculator {
height: 270px;
background: white;
width: 400px;
margin-left: auto;
margin-right:auto;;
}
</style>
<script type="text/javascript">
var calculator = new function() {
this.current = 0;
this.previous = 0;
this.currentOperator = null;
this.calculated = false;
this.sum = function() {
this.currentOperator = '+';
this.previous = this.current;
this.reset();
};
this.reset = function() {
$('.result').html('');
this.calculated = false;
};
this.substract = function() {
this.currentOperator = '-';
this.previous = this.current;
this.reset();
};
this.multiply = function() {
this.currentOperator = 'X';
this.previous = this.current;
this.reset();
};
this.divide = function() {
this.currentOperator = '/';
this.previous = this.current;
this.reset();
};
this.numClick = function(value) {
if(this.calculated == true)
this.reset();
$('.result').html(this.current = Number($('.result').html() + value));
};
this.getResult = function() {
this.calculated = true;
if(this.currentOperator == '+')
$('.result').html(this.current + this.previous);
else if(this.currentOperator == '-')
$('.result').html(this.previous - this.current);
else if(this.currentOperator == 'X')
$('.result').html(this.previous * this.current);
else if(this.currentOperator == '/')
$('.result').html(this.previous / this.current);
};
};
</script>
<div class="calculator">
<div class="header">
<div class="result"></div>
</div>
<div class="body">
<div class="buttons num" onclick="calculator.numClick(1);">1</div>
<div class="buttons num" onclick="calculator.numClick(2);">2</div>
<div class="buttons num" onclick="calculator.numClick(3);">3</div>
<div class="buttons num" onclick="calculator.numClick(4);">4</div>
<div class="buttons num" onclick="calculator.numClick(5);">5</div>
<div class="buttons num" onclick="calculator.numClick(6);">6</div>
<div class="buttons num" onclick="calculator.numClick(7);">7</div>
<div class="buttons num" onclick="calculator.numClick(8);">8</div>
<div class="buttons num" onclick="calculator.numClick(9);">9</div>
<div class="buttons num" onclick="calculator.numClick(0);">0</div>
<div class="buttons operator" onclick="calculator.sum();">+</div>
<div class="buttons operator" onclick="calculator.substract();">-</div>
<div class="buttons operator" onclick="calculator.multiply();">X</div>
<div class="buttons operator" onclick="calculator.divide();">/</div>
<div class="buttons operator" onclick="calculator.getResult();">=</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment