Skip to content

Commit

Permalink
Modified Basic and Multiplication Table Calculator (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuvraj960 authored Feb 15, 2024
1 parent e64117a commit 41f8673
Show file tree
Hide file tree
Showing 10 changed files with 430 additions and 310 deletions.
7 changes: 5 additions & 2 deletions Calculators/Basic-Calculator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Description :-

This calculator will help you calculate your basic arithmetic calculations. This is your user friendly basic old school calculator.
This calculator will help you calculate your basic arithmetic calculations. This is your user friendly basic calculator. It has added functionality of switching between dark and light mode. It has a modern design with a simple and easy to use interface. Best for your daily use.

## Tech Stack :-

Expand All @@ -11,4 +11,7 @@ This calculator will help you calculate your basic arithmetic calculations. This
- JavaScript

## Screenshot :-
![image](https://github.com/Rakesh9100/CalcDiverse/assets/134225415/840879ac-0dab-4977-828c-a48eff70d5a3)
### Dark Mode:
![Calculator-Demo-Image-Dark](image.png) <br><br>
### Light Mode:
![Calculator-Demo-Image-Light](image-2.png)
Binary file removed Calculators/Basic-Calculator/delete.png
Binary file not shown.
Binary file added Calculators/Basic-Calculator/image-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Calculators/Basic-Calculator/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
92 changes: 44 additions & 48 deletions Calculators/Basic-Calculator/index.html
Original file line number Diff line number Diff line change
@@ -1,59 +1,55 @@
<!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>Basic Calculator</title>
<title>Calculator In CSS</title>
<link rel="stylesheet" href="style.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Orbitron:wght@400;800&family=Roboto+Condensed:wght@300;400;700&display=swap" rel="stylesheet">
</head>

<body>
<div class="main">
<h1> Your Basic Old School Calculator</h1>
<div class="box">

<h2>Basic Calculator</h2>
<input type="text" id="display" disabled>
<br>
<div id="history"></div>
<br>
<button onclick="appendToDisplay('1')">1</button>
<button onclick="appendToDisplay('2')">2</button>
<button onclick="appendToDisplay('3')">3</button>
<button onclick="clearDisplay()">C</button>
<button onclick="removeFromDisplay()"><img src = "delete.png"></button>
<br>
<button onclick="appendToDisplay('4')">4</button>
<button onclick="appendToDisplay('5')">5</button>
<button onclick="appendToDisplay('6')">6</button>
<button onclick="appendToDisplay('+')">+</button>
<button onclick="appendToDisplay('-')">-</button>
<br>
<button onclick="appendToDisplay('7')">7</button>
<button onclick="appendToDisplay('8')">8</button>
<button onclick="appendToDisplay('9')">9</button>
<button onclick="appendToDisplay('*')">*</button>
<button onclick="appendToDisplay('/')">/</button>
<br>
<button onclick="appendToDisplay('0')">0</button>
<button onclick="appendToDisplay('.')">.</button>
<button onclick="appendToDisplay('%')">%</button>
<button onclick="appendToDisplay('(')">(</button>
<button onclick="appendToDisplay(')')">)</button>

<br>
<button onclick="appendToDisplay('Math.sqrt(')"></button>
<button onclick="appendToDisplay('**')">^</button>
<button onclick="appendToDisplay('Math.sin(')">sin</button>
<button onclick="appendToDisplay('Math.cos(')">cos</button>
<button onclick="calculate()">=</button>
<br>

</div>
</div>
<div class="btn">
<strong>🌞</strong><strong>🌙</strong>
</div>

<div class="container">
<form class="calculator" name="calc">
<input type="text" class="value" id="display" name="txt">
<span class="num clear" onclick="calc.txt.value = '' "><i>C</i></span>
<span class="num" onclick="calc.txt.value += '(' "><i>(</i></span>
<span class="num" onclick="calc.txt.value += ')' "><i>)</i></span>
<span class="num" onclick="calc.txt.value += '/' "><i>/</i></span>
<span class="num clear-one" onclick="clearOne()"><i>CE</i></span>
<span class="num" onclick="calc.txt.value += '7' "><i>7</i></span>
<span class="num" onclick="calc.txt.value += '8' "><i>8</i></span>
<span class="num" onclick="calc.txt.value += '9' "><i>9</i></span>
<span class="num" onclick="calc.txt.value += '+' "><i>+</i></span>
<span class="num" onclick="calc.txt.value += '*' "><i>*</i></span>
<span class="num" onclick="calc.txt.value += '-' "><i>-</i></span>
<span class="num" onclick="calc.txt.value += '4' "><i>4</i></span>
<span class="num" onclick="calc.txt.value += '5' "><i>5</i></span>
<span class="num" onclick="calc.txt.value += '6' "><i>6</i></span>
<span class="num" onclick="calc.txt.value += 'Math.sqrt(' "><i></i></span>
<span class="num" onclick="calc.txt.value += '%' "><i>%</i></span>
<span class="num" onclick="calc.txt.value += '.' "><i>.</i></span>
<span class="num" onclick="calc.txt.value += '1' "><i>1</i></span>
<span class="num" onclick="calc.txt.value += '2' "><i>2</i></span>
<span class="num" onclick="calc.txt.value += '3' "><i>3</i></span>
<span class="num" onclick="calc.txt.value += 'Math.sin(' "><i>sin</i></span>
<span class="num" onclick="calc.txt.value += 'Math.cos(' "><i>cos</i></span>

<span class="num result-give equal" onclick="document.calc.txt.value=eval(calc.txt.value)"><i>=</i></span>

<span class="num" onclick="calc.txt.value += ',' "><i>,</i></span>
<span class="num" onclick="calc.txt.value += '0' "><i>0</i></span>
<span class="num" onclick="calc.txt.value += 'Math.pow(' "><i>^</i></span>
<span class="num" onclick="calc.txt.value += 'Math.exp(' "><i>exp</i></span>
<span class="num" onclick="calc.txt.value += 'Math.log10(' "><i>log</i></span>
</form>
</div>
<script src="script.js"></script>

</body>

</html>
73 changes: 36 additions & 37 deletions Calculators/Basic-Calculator/script.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
let history = [];
let display = document.getElementById('display');
let historyDiv = document.getElementById('history');
let body = document.querySelector("body");
let btn = document.querySelector(".btn");
let calc = document.forms["calc"];
let txt = calc.elements["txt"];
let math = window.math;
let display = document.getElementById("display");

document.addEventListener('keydown', function(event) {
// Check if the key pressed is alphanumeric or an operator
const validKeys = /^[0-9.\+\-\*\/\(\)\^\%\{\}\[\]&]$/;
if (validKeys.test(event.key)) {
// Append the key to the input box
document.getElementById('display').value += event.key;
} else if(event.key === 'Enter') {
event.preventDefault();
calculate();
} else if(event.key === 'Backspace') {
removeFromDisplay();
}
document.addEventListener("keydown", function (event) {
const validKeys = /^[0-9.\+\-\*\/\(\)\^\%\{\}\[\]&]$/;
if (validKeys.test(event.key)) {
document.getElementById("display").value += event.key;
} else if (event.key === "Enter") {
event.preventDefault();
calculate();
} else if (event.key === "Backspace") {
removeFromDisplay();
}
});

function appendToDisplay(value) {
display.value += value;
}

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

function clearDisplay() {
display.value = '';
display.value = "";
}

function calculate() {
try {
const result = eval(display.value);
display.value = result;
} catch (error) {
display.value = 'Error';
}
try {
const result = eval(display.value);
display.value = result;
} catch (error) {
display.value = "Error";
}
}

btn.onclick = function () {
body.classList.toggle("light");
};

function updateHistory() {
historyDiv.innerHTML = '';
history.forEach(entry => {
const p = document.createElement('p');
p.textContent = entry;
historyDiv.appendChild(p);
});
}
calc.onsubmit = calculate(e);

function clearOne() {
let currentValue = calc.txt.value;
calc.txt.value = currentValue.slice(0, -1);
}
Loading

0 comments on commit 41f8673

Please sign in to comment.