JS Calculator
How do I make an js calculator?
What is a js calculator? How do you make a js calculator? This script and codes were developed by Hantz Pierre on 07 September 2022, Wednesday.
JS Calculator - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>JS Calculator</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div id="calculator"> <div class="top"> <span class="clear">C</span> <div class="screen"></div> </div> <div class="keys"> <span>7</span> <span>8</span> <span>9</span> <span class="operator">+</span> <span>4</span> <span>5</span> <span>6</span> <span class="operator">-</span> <span>1</span> <span>2</span> <span>3</span> <span class="operator">÷</span> <span>0</span> <span>.</span> <span class="eval">=</span> <span class="operator">x</span> </div>
</div> <script src='https://code.jquery.com/jquery-2.2.4.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
JS Calculator - Script Codes CSS Codes
* { margin: 0; padding: 0; box-sizing: border-box; font: normal 25px Arial, sans-serif; -webkit-touch-callout: none; -webkit-user-select: none; user-select: none;
}
html { height: 100%; background: url(https://33.media.tumblr.com/d4a884274dff05760e01bd3bdcc6defb/tumblr_nb1ulnMaP91st5lhmo1_1280.jpg); background-size: cover;
}
#calculator { width: 325px; height: auto; overflow:hidden; margin: 100px auto; padding: 20px 20px 9px; position:relative; background: rgba(0,0,0,0.3); border-radius: 3px; box-shadow: 0px 10px 15px rgba(0, 0, 0, 0.2);
}
.top span.clear { position:absolute; z-index:6;
}
.top .screen { height: 80px; width: 100%; position:absolute; top:0; left:0; padding: 20px 10px; background: rgba(0, 0, 0, 0.2); font-size: 27px; font-weight:normal; line-height: 40px; color: white; text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2); text-align: right; letter-spacing: 1px;
}
.keys, .top {overflow: hidden;}
.keys span, .top span.clear { float: left; position: relative; top: 0; cursor: pointer; width: 66px; height: 56px; background: rgba(0,0,0,0.5); border-radius: 3px; box-shadow: 0px 4px rgba(0, 0, 0, 0.2); margin: 0 7px 11px 0; color: #ccc; line-height: 56px; text-align: center; user-select: none; transition: all 0.2s ease;
}
.keys span.operator { background: rgba(250,100,0,0.3); margin-right: 0;
}
.keys span.eval { color: #888e5f;
}
.top span.clear { background: transparent; box-shadow: none; color: #aaa; width:56px; left:5px
}
.keys span:hover { background: rgba(255,255,255,0.8); color: #333;
}
.keys span.eval:hover { background: #abb850; color: #ffffff;
}
.top span.clear:hover { border-radius:190px; background: rgba(0,0,0,0.1); color: white;
}
.keys span:active { box-shadow: 0px 0px rgba(0, 0, 0, 0.2); top: 4px;
}
.keys span.eval:active { box-shadow: 0px 0px #717a33; top: 4px;
}
.top span.clear:active { top: 4px; box-shadow: 0px 0px #d3545d;
}
JS Calculator - Script Codes JS Codes
'use strict';
// Get all the keys from document
var keys = document.querySelectorAll('#calculator span');
var operators = ['+', '-', 'x', '÷'];
var decimalAdded = false;
// Add onclick event to all the keys and perform operations
for (var i = 0; i < keys.length; i++) { keys[i].onclick = function (e) { // Get the input and button values var input = document.querySelector('.screen'); var inputVal = input.innerHTML; var btnVal = this.innerHTML; // Now, just append the key values (btnValue) to the input string and finally use javascript's eval function to get the result // If clear key is pressed, erase everything if (btnVal == 'C') { input.innerHTML = ''; decimalAdded = false; } // If eval key is pressed, calculate and display the result else if (btnVal == '=') { var equation = inputVal; var lastChar = equation[equation.length - 1]; // Replace all instances of x and ÷ with * and / respectively. This can be done easily using regex and the 'g' tag which will replace all instances of the matched character/substring equation = equation.replace(/x/g, '*').replace(/÷/g, '/'); // Final thing left to do is checking the last character of the equation. If it's an operator or a decimal, remove it if (operators.indexOf(lastChar) > -1 || lastChar == '.') equation = equation.replace(/.$/, ''); if (equation) input.innerHTML = eval(equation); decimalAdded = false; } // Basic functionality of the calculator is complete. But there are some problems like // 1. No two operators should be added consecutively. // indexOf works only in IE9+ else if (operators.indexOf(btnVal) > -1) { // Operator is clicked // Get the last character from the equation var lastChar = inputVal[inputVal.length - 1]; // Only add operator if input is not empty and there is no operator at the last if (inputVal != '' && operators.indexOf(lastChar) == -1) input.innerHTML += btnVal; // Allow minus if the string is empty else if (inputVal == '' && btnVal == '-') input.innerHTML += btnVal; // Replace the last operator (if exists) with the newly pressed operator if (operators.indexOf(lastChar) > -1 && inputVal.length > 1) { // Here, '.' matches any character while $ denotes the end of string, so anything (will be an operator in this case) at the end of string will get replaced by new operator input.innerHTML = inputVal.replace(/.$/, btnVal); } decimalAdded = false; } // Now only the decimal problem is left. We can solve it easily using a flag 'decimalAdded' which we'll set once the decimal is added and prevent more decimals to be added once it's set. It will be reset when an operator, eval or clear key is pressed. else if (btnVal == '.') { if (!decimalAdded) { input.innerHTML += btnVal; decimalAdded = true; } } // if any other key is pressed, just append it else { input.innerHTML += btnVal; } };
}
Developer | Hantz Pierre |
Username | universe360 |
Uploaded | September 07, 2022 |
Rating | 3 |
Size | 4,859 Kb |
Views | 28,336 |
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!
Name | Size |
A Pen by Hantz Pierre | 5,050 Kb |
Weather app v.2 | 4,878 Kb |
Weather app | 3,306 Kb |
Pomodoro Clock | 2,363 Kb |
Search Form Styles | 1,906 Kb |
Random Wise Quotes | 2,881 Kb |
RGB Guessing Game | 4,567 Kb |
FoundataionFrameWorks - Portfolio Template | 2,016 Kb |
Wikipedia search | 4,006 Kb |
Twich JSON API | 1,163 Kb |
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!
Name | Username | Size |
Static Image to Video Block | Grayghostvisuals | 4,864 Kb |
Lunar eclipse | Ademilter | 2,056 Kb |
Flat Star Wars | Kevinjannis | 37,947 Kb |
Simple Login Form Template | Banunn | 3,571 Kb |
Iron Man SVG Loading Animation | Andythayer | 3,069 Kb |
A Pen by Michael Parenteau | Michaelparenteau | 2,133 Kb |
Expandable Left Side Bar with jQuery animate | Retrofuturistic | 2,483 Kb |
Social.svg.min | Larsenwork | 13,849 Kb |
Responsive Section hover effect to show content | Berdejitendra | 2,540 Kb |
Airbnb Homepage | SindhujaD | 2,480 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!