Calculator

Developer
Size
4,572 Kb
Views
6,072

How do I make an calculator?

Calculator for Free Code Camp using JQuery. What is a calculator? How do you make a calculator? This script and codes were developed by Adam on 28 November 2022, Monday.

Calculator Previews

Calculator - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Calculator </title> <link href="https://fonts.googleapis.com/css?family=Archivo+Black|Orbitron" rel="stylesheet"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div> <div class="calc"> <div class="row"> <div class="title">KASIO</div> <div class="solar"> <div></div> <div></div> <div></div> <div></div> </div> <div class="info">Calculator</div> </div> <div class="wrap"> <div class="back-numbers">.00000000</div> <div class="back-numbers2">000000000000000000</div> <div class="screen display">0</div> <div class="screen calc-display"></div> </div> <div class="row"> <button data-key="ac" class="utility">AC</button> <button data-key="c" class="utility">C</button> <button data-key="ce" class="ce">CE</button> <button data-key="%" class="operator">%</button> </div> <div class="row"> <button data-key="7">7</button> <button data-key="8">8</button> <button data-key="9">9</button> <button data-key="/" class="operator">&divide;</button> </div> <div class="row"> <button data-key="4">4</button> <button data-key="5">5</button> <button data-key="6">6</button> <button data-key="*" class="operator">X</button> </div> <div class="row"> <button data-key="1">1</button> <button data-key="2">2</button> <button data-key="3">3</button> <button data-key="-" class="operator">-</button> </div> <div class="row"> <button data-key="0">0</button> <button data-key=".">.</button> <button data-key="=" class="operator">=</button> <button data-key="+" class="operator">+</button> </div> </div> <div class="flip-button">Flip</div>
</div> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Calculator - Script Codes CSS Codes

html { font-size: 20px;
}
body { display: flex; justify-content: center; margin-top: 60px; background-color: #333333; color: white; font-family: 'Archivo Black', sans-serif;
}
.calc { width: 300px; height: 450px; display: flex; flex-direction: column; justify-content: space-around; align-items: center; background: #324159; background: linear-gradient(to bottom right, #324159, #253041); border-radius: 25px; box-shadow: 5px 5px 5px #111111;
}
.row { display: flex; justify-content: space-around; align-items: center; width: 260px; height: 60px;
}
.title { padding-right: 10px; font-size: 1rem; text-shadow: 2px 2px 3px #111111;
}
.info { padding-left: 10px; font-size: 0.8rem; text-shadow: 2px 2px 3px #111111;
}
.solar { display: flex; width: 80px; height: 20px; background-color: #001a00; border-radius: 5px; border: 2px solid black; box-shadow: 2px 2px 0px #253041;
}
.solar > div { width: 20px; height: 20px; border-left: 2px solid black;
}
.solar > div:first-child { border-left: none;
}
.wrap { position: relative; font-family: 'Orbitron', sans-serif; font-size: 30px;
}
.back-numbers { position: absolute; color: rgba(0, 0, 0, 0.05); top: 6px; left: 32px;
}
.back-numbers2 { position: absolute; color: rgba(0, 0, 0, 0.05); top: 50px; left: 17px; font-size: 0.8rem;
}
.screen { display: flex; justify-content: flex-end; text-align: center; height: 30px; width: 250px; padding: 2px 3px; background-color: #bbccbc; color: black; border: 5px solid #253041; border-radius: 8px;
}
.calc-display { display: flex; justify-content: flex-end; align-items: flex-end; margin-top: -7px; margin-bottom: 15px; font-size: 0.8rem; border-top: none; border-radius: 0 0 10px 10px;
}
button { height: 46px; width: 56px; color: white; background-color: #AAAAAA; font-weight: bold; font-size: 1.4rem; font-family: 'gotham rounded', sans-serif; text-shadow: 1px 1px 3px #111111; border-width: 2px 3px 3px 2px; border-color: #999999; border-radius: 10px 10px 20px 20px; cursor: pointer;
}
.utility { background-color: #ed4964; border-color: #a50d30; font-size: 1.3rem;
}
.operator { background-color: #222222; border-color: #444444;
}
.ce { background-color: #68cc9b; border-color: #39ac75; font-size: 1.3rem;
}
.flip { transform: rotate(180deg);
}
.flip-button { width: 100px; margin: 15px 0 0 100px; background: red; text-shadow: 1px 1px 3px #111111; border-width: 2px 3px 3px 2px; border-color: #999999; border-radius: 10px 10px 10px 10px; font-weight: bold; font-size: 1.4rem; font-family: 'gotham rounded', sans-serif; text-align: center; cursor: pointer;
}

Calculator - Script Codes JS Codes

/* JQuery calculator with flip button to write school kid messages*/
$(document).ready(function() { var answer = 0; //Store expression var exp = ""; //Operator array and function to check if user input is an operator var operators = ["+", "-", "*", "/", "%", "="]; function opCheck(input) { for (var i = 0; i < operators.length; i++) { if (input === operators[i]) { return true; } } } //Power On/Off Function var powerOn = false; function power() { if (powerOn === true) { $('.display').text(""); $('.calc-display').text(""); $('button').attr("disabled", true); $("button[data-key='ac']").removeAttr("disabled"); exp = ""; powerOn = false; } else if (powerOn === false) { $('.display').text("0"); $('.calc-display').text(""); $('button').removeAttr("disabled"); exp = "" powerOn = true; } } //Clear function function clear() { $('.display').text("0"); $('.calc-display').text(""); exp = ""; } // Check if input is valid based on previous selection function inputCheck() { var input = $(this).data("key"); //Check power if (input === "ac") { return; } //Check clear if (input === "c") { clear(); return; } //Check delete if (input === "ce") { exp = exp.substring(0, exp.length - 1); $('.calc-display').text(exp); $('.display').text("0"); return; } //Check expression length if (exp.length > 17) { exp = exp.substring(0, exp.length - 1); } //Prevent consecutive operators if (opCheck(input)) { if (opCheck(exp[exp.length - 1])) { exp = exp.substring(0, exp.length - 1) } } //Add input to expression exp += input; //Check if first entry is decimal and prevent multiple decimal points if (exp[0] === ".") { exp = "0."; } if (input === ".") { if (exp.substring(0, exp.length - 1).includes(".")) { exp = exp.substring(0, exp.length - 1); } } //enable operators after a number if (opCheck(exp[0])) { clear(); } //Check and perform percentage calculation if (input === "%") { exp = exp.substring(0, exp.length - 1) answer = eval(exp) / 100; $('.display').text(answer); $('.calc-display').text(exp); exp = ""; return; } //Update displays $('.display').text(exp[exp.length - 1]); $('.calc-display').text(exp); //Return answer and reduce length if needed if (input === "=") { exp = exp.substring(0, exp.length - 1) answer = eval(exp); if (answer.toString().length > 10) { answer = answer.toString().substring(0, 10) } $('.display').text(answer); exp = ""; } } function flip() { $(".calc").toggleClass('flip'); } //Event handlers $(".flip-button").click(flip); $("button").click(inputCheck); $("button[data-key='ac']").click(power);
});
Calculator - Script Codes
Calculator - Script Codes
Home Page Home
Developer Adam
Username rzencoder
Uploaded November 28, 2022
Rating 3
Size 4,572 Kb
Views 6,072
Do you need developer help for 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!

Adam (rzencoder) Script Codes
Create amazing marketing copy 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!