HTML Code for Scientific Calculator

HTML Code for Scientific Calculator
Code Snippet:User Juicy Scientific Calculator
Author: Raphael
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 26,173
License: MIT
Edit Code online: View on CodePen
Read More

This vanilla JavaScript code snippet helps you to create a scientific calculator in your HTML projects. It comes with all necessary functions to perform scientific calculations on numbers. Besides addition, subtraction, multiplication, and division, users can also find the the values of exponents, log, natural log (ln), trig functions.

Basically, this scientific calculator built with HTML, CSS, and JavaScript. So, it can be highly customizable with additional CSS to make it according to your needs.

HTML Code for Scientific Calculator

1. In first step, create the HTML structure for scientific calculator as follows:

<div class="calculator">
        <input type="text" id="screen" maxlength="20">
        <div class="calc-buttons">

<div class="functions-one">
    <button class="button triggers">C</button>
    <button class="button basic-stuff">(</button>
    <button class="button basic-stuff">)</button>
    <button class="button numbers">7</button>
    <button class="button numbers">8</button>
    <button class="button numbers">9</button>
    <button class="button numbers">4</button>
    <button class="button numbers">5</button>
    <button class="button numbers">6</button>
    <button class="button numbers">1</button>
    <button class="button numbers">2</button>
    <button class="button numbers">3</button>
    <button class="button basic-stuff">±</button>
    <button class="button numbers">0</button>
    <button class="button basic-stuff">.</button>
</div>

<div class="functions-two">
    <button class="button triggers"><=</button>
    <button class="button complex-stuff">%</button>
    <button class="button complex-stuff">x !</button>
    <button class="button complex-stuff">x^</button>
    <button class="button basic-stuff">*</button>
    <button class="button basic-stuff">/</button>
    <button class="button complex-stuff">ln</button>
    <button class="button complex-stuff">e</button>
    <button class="button basic-stuff">+</button>
    <button class="button complex-stuff">x ²</button>
    <button class="button complex-stuff">log</button>
    <button class="button complex-stuff">cos</button>
    <button class="button basic-stuff">-</button>
    <button class="button complex-stuff">√</button>
    <button class="button complex-stuff">sin</button>
    <button class="button complex-stuff">tan</button>
    <button class="button triggers">=</button>
    <button class="button complex-stuff">π</button>
    <button class="button complex-stuff">∘</button>
    <button class="button complex-stuff">rad</button>
</div>
      </div>
</div>

2. After that, style the calculator using the following CSS.

.calculator {
max-width: 530px;
background-color: beige;
padding: 20px;
border-radius: 10px;
margin: 10px auto;
}
input[type=text] {
width: 315px;
    height: 25px;
    border-radius: 5px;
    border: 0px;
    background-color: #333333;
    color: #d9d9d9;
    padding: 0 5px 0 5px;
    margin: 0 0px 10px 84px;

}
.calc-buttons {
    display: flex;
flex-wrap: wrap;
justify-content: space-between;

}
.button {
   margin: 3px;
width: 63px;
border: none;
height: 25px;
border-radius: 4px;
color: #000000;
cursor: pointer;
}
button:hover {
  background-color: hsla(180, 100%, 40%, 0.3);
  transition: .2s;
}
.functions-one {
    width: 210px;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-evenly;
}

.functions-two {
width: 280px;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
}
.triggers {
    background-color: #ffc266;
}
.numbers {
    background-color: #999999;
}
.basic-stuff {
    background-color: #80d4ff;
}

.complex-stuff {
    background-color: #80ffff;
}

3. Finally, add the following JavaScript program for Scientific Calculator calculator and done.

var display = document.getElementById("screen");
var buttons = document.getElementsByClassName("button");
  
  Array.prototype.forEach.call(buttons, function(button) {
  button.addEventListener("click", function() {
    if (button.textContent != "=" && 
    button.textContent != "C" && 
    button.textContent != "x" && 
    button.textContent != "÷" && 
    button.textContent != "√" && 
    button.textContent != "x ²" && 
    button.textContent != "%" && 
    button.textContent != "<=" && 
    button.textContent != "±" && 
    button.textContent != "sin" && 
    button.textContent != "cos" && 
    button.textContent != "tan" && 
    button.textContent != "log" && 
    button.textContent != "ln" && 
    button.textContent != "x^" && 
    button.textContent != "x !" && 
    button.textContent != "π" && 
    button.textContent != "e" && 
    button.textContent != "rad" 
    && button.textContent != "∘") {
      display.value += button.textContent;
    } else if (button.textContent === "=") {
      equals();
    } else if (button.textContent === "C") {
      clear();
    } else if (button.textContent === "x") {
      multiply();
    } else if (button.textContent === "÷") {
      divide();
    } else if (button.textContent === "±") {
      plusMinus();
    } else if (button.textContent === "<=") {
      backspace();
    } else if (button.textContent === "%") {
      percent();
    } else if (button.textContent === "π") {
      pi();
    } else if (button.textContent === "x ²") {
      square();
    } else if (button.textContent === "√") {
      squareRoot();
    } else if (button.textContent === "sin") {
      sin();
    } else if (button.textContent === "cos") {
      cos();
    } else if (button.textContent === "tan") {
      tan();
    } else if (button.textContent === "log") {
      log();
    } else if (button.textContent === "ln") {
      ln();
    } else if (button.textContent === "x^") {
      exponent();
    } else if (button.textContent === "x !") {
      factorial();
    } else if (button.textContent === "e") {
      exp();
    } else if (button.textContent === "rad") {
      radians();
    } else if (button.textContent === "∘") {
      degrees();
    }
  });
});


function syntaxError() {
  if (eval(display.value) == SyntaxError || eval(display.value) == ReferenceError || eval(display.value) == TypeError) {
    display.value == "Syntax Error";
  }
}


function equals() {
  if ((display.value).indexOf("^") > -1) {
    var base = (display.value).slice(0, (display.value).indexOf("^"));
    var exponent = (display.value).slice((display.value).indexOf("^") + 1);
    display.value = eval("Math.pow(" + base + "," + exponent + ")");
  } else {
    display.value = eval(display.value)
    checkLength()
    syntaxError()
  }
}

function clear() {
  display.value = "";
}

function backspace() {
  display.value = display.value.substring(0, display.value.length - 1);
}

function multiply() {
  display.value += "*";
}

function divide() {
  display.value +=  "/";
}

function plusMinus() {
  if (display.value.charAt(0) === "-") {
    display.value = display.value.slice(1);
  } else {
    display.value = "-" + display.value;
  }
}

function factorial() {
  var number = 1;
  if (display.value === 0) {
    display.value = "1";
  } else if (display.value < 0) {
    display.value = "undefined";
  } else {
    var number = 1;
    for (var i = display.value; i > 0; i--) {
      number *=  i;
    }
    display.value = number;
  }
}

function pi() {
  display.value = (display.value * Math.PI);
}

function square() {
  display.value = eval(display.value * display.value);
}

function squareRoot() {
  display.value = Math.sqrt(display.value);
}

function percent() {
  display.value = display.value / 100;
}

function sin() {
  display.value = Math.sin(display.value);
}

function cos() {
  display.value = Math.cos(display.value);
}

function tan() {
  display.value = Math.tan(display.value);
}

function log() {
  display.value = Math.log10(display.value);
}

function ln() {
  display.value = Math.log(display.value);
}

function exponent() {
  display.value += "^";
}

function exp() {
  display.value = Math.exp(display.value);
}

function radians() {
  display.value = display.value * (Math.PI / 180);
}

function degrees() {
  display.value = display.value * (180 / Math.PI);
}

That’s all! hopefully, you have successfully created a scientific calculator. If you have any questions or suggestions, let me know by comment below.

3 thoughts on “HTML Code for Scientific Calculator”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X