Simple Calculator in JavaScript Source Code Free Download

In JavaScript, Simple Calculator in JavaScript Source Code is a web-based calculator which is developed using JavaScript, CSS, bootstrap, and HTML.

This Calculator Source Code in JavaScript is a real-life simple calculator. The Calculator is for solving the mathematical calculations of numbers. You can see the basic features of the calculator in this project.

What is Calculator In JavaScript?

The Simple Calculator in JavaScript Source Code is a project that can calculate numbers on web which can performs various mathematical functions such as (addition, multiplication, subtraction and multiplication).

In addition, the Calculator in JavaScript has the following calculate function and features such as the user can add, subtract, divide, multiply, even check the remainder of any two numbers.

And you can just click the numbers you want to involve in calculations and click the button with “=” sign for the result.

This Simple Calculator project with source code also includes a downloadable source code for free, just find the downloadable source code below and click to start downloading.

But if you want to make it lessen you can copy and paste the source code below.

I have here a suggested list of Best JavaScript Projects with Source Code Free to download and I’m sure this can help you to improve your skills in JavaScript programming and web development as a whole.

To start creating a JavaScript Calculator, makes sure that you have any platform in creating a JavaScript, CSS, bootstrap, and HTML installed in your computer, in my case I will use Sublime Text.

Project Details and Technology:

ABOUT PROJECTPROJECT DETAILS
Project Name :Calculator In JavaScript
Project Platform :JavaScript
Programming Language Used:JavaScript, HTML, and CSS
Developer Name :itsourcecode.com
IDE Tool (Recommended):Sublime
Project Type :Web Application
Database:None
Upload Date:9 / 4 / 2022

Steps on how to create a Calculator In JavaScript Source Code

Time needed: 5 minutes

Calculator In JavaScript

  • Step 1: Open Sublime Text.

    First after installing sublime text IDE, click “open” to start.
    step 1

  • Step 2: Create a HTML file.

    Second click “file” and select “save as” and named it “index.html
    calculator html file

  • Step 3: Create a CSS file.

    Third click “file” and select “save as” and named it “style.css
    calculator css file

  • Step 4: Create a JavaScript file.

    Fourth click “file” and select “save as” and named it “app.js
    calculator javascript file

  • Step 5: The actual code.

    You are free to copy the code given below and paste to your different file created.

The Code Given Below Is For The Index module

<html>
    <head>
		<link rel="stylesheet" type="text/css" href="styles.css">
		<link href="https://fonts.googleapis.com/css?family=Open+Sans:600,700" rel="stylesheet">
		<title>A simple calculator</title>
	</head>
    <body>
          <!-- div class -->
        <div id="container">
            <div id="calculator">
                <div id="result">
                    <div id="history">
                        <p id="history-value"></p>
                    </div>
                    <div id="output">
                        <p id="output-values"></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="*">&times;</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="script.js"></script>
    </body>
</html>

In this module which is the index of the system project.

The Code Given Below Is For The Module Script.js

function getHistory(){
    return document.getElementById("history-value").innerText;
}
function printHistory(num){
    document.getElementById("history-value").innerText=num;
}
function getOutput(){
    return document.getElementById("output-values").innerText;
}
function printOutput(num){
    if(num==""){
    document.getElementById("output-values").innerText=num;
    }
    else{
        document.getElementById("output-values").innerText=getFormattedNumber(num);
    }
}
function getFormattedNumber(num){
    if(num=="-"){
        return "";
    }
    var n=Number(num);
    var value= n.toLocaleString("en");
    return value;
}
function reverseNumberFormat(num){
    return Number(num.replace(/,/g, ''));
}
var operator = document.getElementsByClassName("operator");
for( var i=0;i<operator.length;i++){
    operator[i].addEventListener('click',function(){
            if(this.id=="clear"){
                printHistory("");
                printOutput("");
            }
            else if(this.id=="backspace"){
                var
                output=reverseNumberFormat(getOutput()).toString();
                if(output){//if output has a value
                    output= output.substr(0,output.length-1);
                printOutput(output);
                }
            }
            else{
                var output = getOutput();
                var history = getHistory();
                if(output==""&&history!=""){
                    if(isNaN(history[history.length-1])){
                        history=history.substr(0,history.length-1);
                    }
                }
                if(output!="" || history!=""){
                    output= output==""?
                    output:reverseNumberFormat(output);
                    history=history+output;
                    if(this.id=="="){
                        var result= eval(history);
                        printOutput(result);
                        printHistory("");
                    }
                    else{
                        history=history+this.id;
                        printHistory(history);
                        printOutput("");
                    }
                }
            }
    });
}
var number = document.getElementsByClassName("number");
for(var i=0;i<number.length;i++){
    number[i].addEventListener('click',function(){
        var output=reverseNumberFormat(getOutput());
        if(output!=NaN){
            output=output+this.id;
            printOutput(output);
        }
    });
}

In this module which is the JavaScript file of the system.

The Code Given Below Is For The Module Style.css

body{
	font-family: 'Open Sans',sans-serif;
	background-color:black;
}
#container{
	width: 1000px;
	height: 550px;
	background-image:linear-gradient(rgba(0,0,0,0.6),rgba(0,0,0,0.6)), url(Sugar-Excipients.jpg);
	margin: 20px auto;	
}
#calculator{
	width: 320px;
	height: 520px;
    background-color: #eaedef;
    margin: 0 auto;
    top: 20px;
    position: relative;
    border-radius: 5px;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
}
#result{
    height: 120px;
}
#history{
    text-align: right;
    height: 20px;
    margin: 0 20px;
    padding-top: 20px;
    font-size: 15px;
    color: #919191;
}
#output{
    text-align: right;
    height: 60px;
    margin: 10px 20px;
    font-size: 30px;

}
#keyboard{
    height: 400px;
}
.operator, .number, .empty{
    width: 50px;
    height: 50px;
    margin: 15px;
    float: left;
    border-radius: 50%;
    border-width: 0;
    font-size: 15px;
    font-weight: bold;

}
.number, .empty{
    background-color: #eaedef;
}
.operator{
    background-color:lightgrey;
}
.number, .operator{
    cursor: pointer;
}
.number:active, .operator:active{
    font-size: 13px;
}
.number:focus, .operator:focus, .empty:focus{
    outline: 0;
}
button:nth-child(4){
    font-size: 20px;
    background-color: #20b2aa;
}
button:nth-child(8){
    font-size: 20px;
    background-color: #ffa500;
}
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;
}



In this module which is the CSS file of the system.

Downloadable Source Code

Summary

This JavaScript Project With Source Code is simply in HTML, CSS, and JavaScript.

Taking about the features of this system, the user can add, subtract, divide, multiply, even check the remainder of any two numbers.

You can just click the numbers you want to involve in calculations and click the button with “=” sign for the result.

This project includes a lot of JavaScript for making the functioning of the project.

Related Articles

Inquiries

If you have any questions or suggestions about Simple Calculator in JavaScript Source Code, please feel free to leave a comment below.

Leave a Comment