<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Calculator</title>
<!-- CSS Calculator -->
<style>
body {
background-color: #fff;
background: black;
}
h1 {
text-align: center;
/* suppose to be right */
font-size: 40px;
color: white;
font-family: 'Courier New', Courier, monospace;
font-weight: bold;
}
#container {
width: 1000px;
height: 600px;
margin: 20px auto;
width: 100%;
}
#calculator {
width: 320px;
height: 520px;
background-color: #eaedef;
margin: 0 auto;
top: 20px;
position: relative;
border-radius: 8px;
box-shadow: 0 4px 8px 0 rgb(35, 170, 194), 0 6px 20px 0 rgb(18, 163, 105);
}
#result {
height: 120px;
}
#keyboard {
height: 400px;
}
#history {
text-align: right;
height: 20px;
margin: 0 20px;
padding-top: 20px;
font-size: 20px;
color: #919191;
}
#output {
text-align: right;
height: 60px;
margin: 10px 20px;
font-size: 35px;
}
.operator,
.number,
.empty {
width: 50px;
height: 50px;
margin: 15px;
float: left;
border-radius: 50%;
border-width: 0;
font-weight: bold;
font-size: 15px;
}
.number,
.operator {
cursor: pointer;
}
.operator:active,
.number:active {
font-size: 12px;
}
.number,
.empty {
background-color: #eaedef;
}
.operator:focus,
.number:focus,
.empty:focus {
outline: 0;
}
button:nth-child(3) {
background-color: #ffa500;
}
button:nth-child(1) {
background-color: #ffa500;
}
button:nth-child(4) {
font-size: 20px;
background-color: #20b2aa;
}
button:nth-child(8) {
font-size: 20px;
background-color: #67bb52;
}
button:nth-child(12) {
font-size: 20px;
background-color: #f08080;
}
button:nth-child(16) {
font-size: 20px;
background-color: #7d93e0;
}
button:nth-child(20) {
font-size: 20px;
background-color: #9477af;
}
</style>
</head>
<!-- HTML Calculator -->
<body>
<h1> Calculator</h1>
<div id="container">
<div id="calculator">
<div id="result">
<div id="history">
<p id="history-value"></p>
</div>
<div id="output">
<p id="output-value"></p>
</div>
</div>
<div id="keyboard">
<button class="operator" id="clear">C</button>
<button class="operator" id="backspace">CE</button>
<button class="operator" id="%">%</button>
<button class="operator" id="/">÷</button>
<button class="number" id="7">7</button>
<button class="number" id="8">8</button>
<button class="number" id="9">9</button>
<button class="operator" id="*">×</button>
<button class="number" id="4">4</button>
<button class="number" id="5">5</button>
<button class="number" id="6">6</button>
<button class="operator" id="-">-</button>
<button class="number" id="1">1</button>
<button class="number" id="2">2</button>
<button class="number" id="3">3</button>
<button class="operator" id="+">+</button>
<button class="empty" id="empty"></button>
<button class="number" id="0">0</button>
<button class="empty" id="empty"></button>
<button class="operator" id="=">=</button>
</div>
</div>
</div>
<script src="main.js"></script>
</body>
</html>
function getPrevious() {
return document.getElementById("history-value").innerText;
}
function printPrevious(num) {
document.getElementById("history-value").innerText = num;
}
function getOutput() {
return document.getElementById("output-value").innerText;
}
function printOutput(num) {
if (num == "") {
document.getElementById("output-value").innerText = num;
}
else {
document.getElementById("output-value").innerText = FormatNumber(num);
}
}
function FormatNumber(num) {
if(num =="-")
{
return "";
}
var temp = Number(num);
var val = temp.toLocaleString("en");
return val;
}
function reverseFormat(num) {
return Number(num.replace(/,/g, ''));
}
///Operator , Clear and backspace functionality.
var ops = document.getElementsByClassName("operator");
for (var i = 0; i < ops.length; i++) {
ops[i].addEventListener('click', function () {
if (this.id == "clear") {
printPrevious("");
printOutput("");
}
else if (this.id == "backspace") {
var clearNum = reverseFormat(getOutput()).toString();
if (clearNum) {
clearNum = clearNum.substr(0, clearNum.length - 1);
printOutput(clearNum);
}
}
else
{
var output =getOutput();
var previous =getPrevious();
if(output=="" && previous !="")
{
if(isNaN(previous[previous.length-1]))
{
previous = previous.substr(0,previous.length-1);
}
}
if(output != "" || previous !="")
{
output=output==""?output:reverseFormat(output);
previous=previous + output;
if (this.id=="=")
{
var result=eval(previous);
printOutput(result);
printPrevious("");
}
else
{
previous=previous + this.id;
printPrevious(previous);
printOutput("");
}
}
}
});
}
var num = document.getElementsByClassName("number");
for (var i = 0; i < num.length; i++) {
num[i].addEventListener('click', function () {
var numOutput = reverseFormat(getOutput());
if (numOutput != NaN) {
numOutput = numOutput + this.id;
printOutput(numOutput);
}
});
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.