JavaScript - Simple Basic Calculator
Submitted by razormist on Sunday, June 9, 2019 - 08:02.
In this tutorial we will create a Simple Basic Calculator using JavaScript. This code automatically generate a calculator app when the user run the web application. The code itself use onclick() function to initiate a several method by means of computing a number when you pass a value as parameter.. You can apply this script to your code, it is a user-friendly program feel free to modify it.
We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer.
.
There you have it we successfully created a Simple Basic Calculator using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
Getting Started:
First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.The Main Interface
This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-6 well" style="background-color:green;">
- <form name="calculator">
- <input type="text" id="result" style="width:100%; height:50px; font-size:30px;">
- </form>
- </div>
- </body>
- </html>
Creating the Script
This code contains the script of the application. This code will calculate your equation when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory- function calculate(val){
- document.getElementById("result").value+=val;
- }
- function cancel(){
- document.getElementById("result").value='';
- }
- function solve(){
- var number = document.getElementById("result").value;
- var total = eval(number);
- document.getElementById("result").value = total;
- }
Add new comment
- 174 views