Plain JavaScript Calculator

Developer
Size
3,600 Kb
Views
40,480

How do I make an plain javascript calculator?

Calculator implemented using plain JS and flex layout.. What is a plain javascript calculator? How do you make a plain javascript calculator? This script and codes were developed by Mnicpt on 30 July 2022, Saturday.

Plain JavaScript Calculator Previews

Plain JavaScript Calculator - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Plain JavaScript Calculator</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div class="app"> <div id="calculator"> <div class="screen"> <span id="result" class="result"></span> </div> <div class="keys"> <div class="btn clear" onclick="calc.clear()">C</div> <div class="btn plusminus">+</div> <div class="btn operator" onclick="calc.operate('/')">/</div> <div class="btn" onclick="calc.digit('7')">7</div> <div class="btn" onclick="calc.digit('8')">8</div> <div class="btn" onclick="calc.digit('9')">9</div> <div class="btn operator" onclick="calc.operate('*')">x</div> <div class="btn" onclick="calc.digit('4')">4</div> <div class="btn" onclick="calc.digit('5')">5</div> <div class="btn" onclick="calc.digit('6')">6</div> <div class="btn operator" onclick="calc.operate('-')">-</div> <div class="btn" onclick="calc.digit('1')">1</div> <div class="btn" onclick="calc.digit('2')">2</div> <div class="btn" onclick="calc.digit('3')">3</div> <div class="btn operator" onclick="calc.operate('+')">+</div> <div class="btn" onclick="calc.digit('0')">0</div> <div class="btn" onclick="calc.digit('.')">.</div> <div class="btn equals" onclick="calc.equals()">=</div> </div> </div>
</div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Plain JavaScript Calculator - Script Codes CSS Codes

@import url(http://fonts.googleapis.com/css?family=Open+Sans);
* { box-sizing: border-box; font-size: 20px;
}
html { background: #ccc; height: 100%; font-family: 'Open Sans', sans-serif;
}
#calculator { background: #efefef; width: 250px; margin: 20px auto; border: 4px solid orange; border-radius: 8px; box-shadow: 0 0 10px #aaa, inset 0 0 2px #333; padding: 10px;
}
.screen { background: -webkit-linear-gradient(top, rgba(30, 87, 153, 0.4) 0%, rgba(41, 137, 216, 0.4) 50%, rgba(32, 124, 202, 0.4) 51%, rgba(125, 185, 232, 0.4) 100%); flex: 1 100%; border: 1px solid #777; height: 45px; border-radius: 2px; line-height: 30px; margin: 5px; padding: 5px;
}
.result { font-size: 35px; float: right;
}
.keys { -ms-box-orient: horizontal; display: -ms-flexbox; display: -moz-flex; display: -webkit-flex; display: flex; -webkit-justify-content: space-around; justify-content: space-around; -webkit-flex-flow: row wrap; flex-flow: row wrap; padding-top: 10px; width: 100%;
}
.btn { background: #fff; -webkit-flex: 1 20%; width: 40px; height: 40px; margin: 5px; cursor: pointer; border-radius: 5px; border: 1px solid #aaa; padding: 10px; text-align: center; line-height: 18px;
}
.btn:hover { background: #ccc;
}
.clear { -webkit-flex: 2 43%;
}
.plusminus { position: relative; padding-top: 7px;
}
.plusminus:after { position: absolute; top: 10px; left: 18px; content: "_";
}
.operator { background: #bbb;
}
.equals { background: orange;
}
.equals:hover { background: darkorange;
}

Plain JavaScript Calculator - Script Codes JS Codes

function CalcCtrl(output) { var stack = [], operationPressed = false, result = "0"; output.innerHTML = "0"; this.digit = function(num) { if(result === "0") { result = num; } else if(operationPressed) { result = num; } else { result += num; } operationPressed = false; output.innerHTML = result; }; this.clear = function() { result = "0"; stack = []; operationPressed = false; output.innerHTML = result; }; this.operate = function(operand) { stack.push(result); stack.push(operand); operationPressed = true; }; this.equals = function() { var i, expression = ""; stack.push(result); for(i = 0; i < stack.length; i++) { expression += stack[i]; } result = eval(expression); stack = []; operationPressed = true; output.innerHTML = result; };
};
var calc = new CalcCtrl(document.querySelector('#result'));
Plain JavaScript Calculator - Script Codes
Plain JavaScript Calculator - Script Codes
Home Page Home
Developer Mnicpt
Username mnicpt
Uploaded July 30, 2022
Rating 3
Size 3,600 Kb
Views 40,480
Do you need developer help for Plain JavaScript Calculator?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Mnicpt (mnicpt) Script Codes
Create amazing blog posts with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!