Calculator – JavaScript

Preview

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Calculator</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <div class="container box">
      <div class="calculator">
        <div class="result">
          <p></p>
        </div>
        <div class="clear">C</div>
        <ul class="numbers keys">
          <li>1</li>
          <li>2</li>
          <li>3</li>
          <li class="orangeColor">+</li>
          <li>4</li>
          <li>5</li>
          <li>6</li>
          <li class="orangeColor">-</li>
          <li>7</li>
          <li>8</li>
          <li>9</li>
          <li class="orangeColor">x</li>
          <li>0</li>
          <li class="orangeColor">.</li>
          <li class="equals">=</li>
          <li class="orangeColor">÷</li>
        </ul>
      </div>
    </div>
    <script src="script.js"></script>
  </body>
</html>

CSS

@import url("https://fonts.googleapis.com/css2?family=Poppins&display=swap");

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: "poppins", sans-serif;
  font-size: 10px;
}

p {
  font-size: 1.5rem;
  line-height: 1.5;
}

.container {
  width: 30%;
  margin: 0 auto;
  padding: 0 20px;
}

.box {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.calculator {
  width: 20rem;
  margin: 0 auto;
  background-color: white;
  padding: 1.5rem;
  border-radius: 5px;
  position: relative;
  border: 3px solid #ffe8cc;
}

.result {
  color: #212529;
  background-color: #ffe8cc;
  height: 3.5rem;
  width: 67%;
  border-radius: 5px;
  margin-bottom: 5px;
  padding: 1rem;
  overflow: hidden;
}

.clear {
  width: 18%;
  background-color: #e8590c;
  padding: 0.5rem;
  position: absolute;
  right: 2rem;
  top: 1.6rem;
  border-radius: 3px;
  color: white;
  cursor: pointer;
  text-align: center;
  font-size: 1.6rem;
}

.keys {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  font-size: 1.6rem;
}

.keys li {
  list-style: none;
  width: 3rem;
  height: 3rem;
  background-color: white;
  margin: 0.5rem;
  padding: 0.3rem;
  border-radius: 3px;
  color: #212529;
  text-align: center;
  cursor: pointer;
  transition: all 0.1s;
}

.keys li:hover {
  background-color: #5f3dc4;
  color: white;
}

.keys .equals {
  background-color: #e8590c;
  color: white;
}

.orangeColor {
  color: #e8590c !important;
}

.orangeColor:hover {
  color: white !important;
}

JavaScript

const keys = document.getElementsByTagName("li");
const result = document.querySelector("p");
const clear = document.querySelector(".clear");

for (let i = 0; i < keys.length; i++) {
  if (keys[i].innerHTML === "=") {
    keys[i].addEventListener("click", calculate);
  } else {
    keys[i].addEventListener("click", addToCurrentValue(i));
  }
}

function addToCurrentValue(i) {
  return () => {
    if (keys[i].innerHTML === "÷") {
      result.innerHTML += "/";
    } else if (keys[i].innerHTML === "x") {
      result.innerHTML += "*";
    } else {
      result.innerHTML += keys[i].innerHTML;
    }
  };
}

function calculate() {
  return (result.innerHTML = eval(result.innerHTML));
}

clear.addEventListener("click", () => {
  result.innerHTML = "";
});

Latest

Leave A Reply

Please enter your comment!
Please enter your name here