Skip to content

Commit

Permalink
Merge pull request #32 from mohitsaini07/mohit-saini
Browse files Browse the repository at this point in the history
change the UI and add sound functionality on click
  • Loading branch information
astush1001 authored Oct 5, 2023
2 parents 864bfbd + f9a88c6 commit 866b89c
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 134 deletions.
12 changes: 7 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@
<link href="utils.css" rel="stylesheet" type="text/css" />
</head>

<body >
<body>

<img src="images/moon.png" id="icon">

<div class="cal-outer-body">
<div class="cal-inner-body">
<h1 class="text-center">Welcome to Calculate me!</h1>
<div class="seperator"></div>
<div class="container flex flex-col mx-auto items-center">
<div class="container flex flex-col items-center">
<audio src="./sound-effect/click-sound.wav" id="click-sound"></audio>

<div class="row">
<input type="text" class="input">
</div>
Expand Down Expand Up @@ -65,7 +66,8 @@ <h1 class="text-center">Welcome to Calculate me!</h1>
</div>

<div class="history">
<h2 class="historyHeadline">Calculation History <a class="clearHistory" title="Click to clear Calculation History">🗑</a></h2>
<h2 class="historyHeadline">Calculation History <a class="clearHistory"
title="Click to clear Calculation History">🗑</a></h2>
<ul id="history-list"></ul>
</div>
</div>
Expand All @@ -74,4 +76,4 @@ <h2 class="historyHeadline">Calculation History <a class="clearHistory" title="C
<script src="script.js"></script>
</body>

</html>
</html>
182 changes: 100 additions & 82 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,118 +1,136 @@
let calculationString = "";
let calculationHistory = []; // To store the calculation history
const maxHistoryItems = 8;
const input = document.querySelector('.input');

const input = document.querySelector(".input");

//dark theme functionality:
const icon = document.getElementById("icon");
icon.onclick = function () {
document.body.classList.toggle("dark-theme");
if (document.body.classList.contains("dark-theme")) {
icon.src = "images/sun.png";
} else {
icon.src = "images/moon.png";
}
}

document.body.classList.toggle("dark-theme");
if (document.body.classList.contains("dark-theme")) {
icon.src = "images/sun.png";
} else {
icon.src = "images/moon.png";
}
};

// Function to update the input field and store history
function updateInputAndHistory() {
document.querySelector('input').value = calculationString;
localStorage.setItem('calculationHistory', JSON.stringify(calculationHistory));
updateHistoryList(); // Update the history list
document.querySelector("input").value = calculationString;
localStorage.setItem(
"calculationHistory",
JSON.stringify(calculationHistory)
);
updateHistoryList(); // Update the history list
}

// Load calculation history from local storage if it exists
const savedCalculationHistory = localStorage.getItem('calculationHistory');
const savedCalculationHistory = localStorage.getItem("calculationHistory");
if (savedCalculationHistory) {
calculationHistory = JSON.parse(savedCalculationHistory);
updateHistoryList(); // Update the history list when the page loads
calculationHistory = JSON.parse(savedCalculationHistory);
updateHistoryList(); // Update the history list when the page loads
}

function updateHistoryList() {
const historyList = document.getElementById('history-list');
historyList.innerHTML = ""; // Clear the existing list
const historyList = document.getElementById("history-list");
historyList.innerHTML = ""; // Clear the existing list

calculationHistory.forEach((calculation) => {
const listItem = document.createElement('li');
listItem.textContent = calculation;
historyList.appendChild(listItem);
});
calculationHistory.forEach((calculation) => {
const listItem = document.createElement("li");
listItem.textContent = calculation;
historyList.appendChild(listItem);
});
}

let buttons = document.querySelectorAll('.button');
let buttons = document.querySelectorAll(".button");
Array.from(buttons).forEach((button) => {
button.addEventListener('click', (e) => {
if (e.target.innerHTML == '=') {
calculate();
} else if (e.target.innerHTML == 'C') {
calculationString = "";
updateInputAndHistory();
} else if (e.target.innerHTML == '←') {
calculationString = calculationString.slice(0, calculationString.length - 1);
updateInputAndHistory();
} else {
calculationString += e.target.innerHTML;
updateInputAndHistory();
}
});
button.addEventListener("click", (e) => {
if (e.target.innerHTML == "=") {
calculate();
} else if (e.target.innerHTML == "C") {
calculationString = "";
updateInputAndHistory();
} else if (e.target.innerHTML == "←") {
calculationString = calculationString.slice(
0,
calculationString.length - 1
);
updateInputAndHistory();
} else {
calculationString += e.target.innerHTML;
updateInputAndHistory();
}
});
});

function calculate() {
calculationString = input.value;
try {
if (calculationString && calculationString.match(/[+\-*\/]/g)) {
let result = eval(calculationString);
calculationHistory.push(`${calculationString} = ${result}`);
calculationString = result.toString();
updateInputAndHistory();
}

} catch (e) {
input.value = "";
input.classList.add('shake'); // Add shake animation class
setTimeout(() => {
input.classList.remove('shake'); // Remove shake animation class after animation
}, 500);
calculationString = input.value;
try {
if (calculationString && calculationString.match(/[+\-*\/]/g)) {
let result = eval(calculationString);
calculationHistory.push(`${calculationString} = ${result}`);
calculationString = result.toString();
updateInputAndHistory();
}
} catch (e) {
input.value = "";
input.classList.add("shake"); // Add shake animation class
setTimeout(() => {
input.classList.remove("shake"); // Remove shake animation class after animation
}, 500);
}
}

input.addEventListener('keydown', function (e) {
if (!isValid(e.key)) {
input.classList.add('shake'); // Add shake animation class
setTimeout(() => {
input.classList.remove('shake'); // Remove shake animation class after animation
}, 500);
e.preventDefault();
}
if(e.key=="Enter"){
calculate();
}
input.addEventListener("keydown", function (e) {
if (!isValid(e.key)) {
input.classList.add("shake"); // Add shake animation class
setTimeout(() => {
input.classList.remove("shake"); // Remove shake animation class after animation
}, 500);
e.preventDefault();
}
if (e.key == "Enter") {
calculate();
}
});


function isValid(key) {
return !isNaN(parseFloat(key)) || ['+', '-', '*', '/', '=', 'Enter', 'Backspace','Shift','.'].includes(key);
return (
!isNaN(parseFloat(key)) ||
["+", "-", "*", "/", "=", "Enter", "Backspace", "Shift", "."].includes(key)
);
}

/** clear Calculation History **/
const clearBtn = document.querySelector('.clearHistory');
clearBtn.addEventListener('click', function(e){
localStorage.clear();
calculationHistory = [];
clearHistory('history-list');
})
function clearHistory(parentId){
const parent = document.querySelector(`#${parentId}`);
while (parent.lastChild) {
parent.removeChild(parent.lastChild);
}
if (e.key == "Enter") {
calculate();
}
};
const clearBtn = document.querySelector(".clearHistory");
clearBtn.addEventListener("click", function (e) {
localStorage.clear();
calculationHistory = [];
clearHistory("history-list");
});
function clearHistory(parentId) {
const parent = document.querySelector(`#${parentId}`);
while (parent.lastChild) {
parent.removeChild(parent.lastChild);
}
if (e.key == "Enter") {
calculate();
}
}

function isValid(key) {
return !isNaN(parseFloat(key)) || ['+', '-', '*', '/', '=', 'Enter', 'Backspace', 'Shift', '.'].includes(key);
return (
!isNaN(parseFloat(key)) ||
["+", "-", "*", "/", "=", "Enter", "Backspace", "Shift", "."].includes(key)
);
}

//Onclick Sound functionality:
const click_sound = document.querySelector("#click-sound");

buttons.forEach((button) => {
button.addEventListener("click", () => {
click_sound.currentTime = 0;
click_sound.play();
});
});
Binary file added sound-effect/click-sound.wav
Binary file not shown.
75 changes: 29 additions & 46 deletions style.css
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
@import url("https://fonts.googleapis.com/css2?family=Belanosima&display=swap");

:root {
--primary-color: #ffdffd;
--secondary-color: #212121;
--primary-color: #e3f9ff;
--secondary-color: rgb(35, 67, 88);
}
.dark-theme {
--primary-color: #212121;
--secondary-color: #ffdffd;
--secondary-color: #141013;
}

body {
height: 100%;
width: 100%;
font-family: "Belanosima", sans-serif;
background-color: var(--primary-color);
}
Expand All @@ -23,22 +22,28 @@ body {
height: 30px;
cursor: pointer;
}

.historyHeadline {
text-align: center;
}

.clearHistory {
cursor: pointer;
}

.button {
padding: 20px;
margin: 0 3px;
border: 2px solid black;
border-radius: 42px;
font-weight: bold;
background-color: #81eded;
width: 50px;
height: 50px;
border: none;
outline: none;
padding: 10px;
border-radius: 0.5rem;
margin: 0.3rem;
box-shadow: 2px 5px 20px #000;
background: transparent;
font-size: 20px;
color: #fff;
cursor: pointer;
width: 66px;
}

.button.arithmetic {
Expand All @@ -47,12 +52,9 @@ body {
}

/* Responsive styles for smaller screens */
@media (max-width: 768px) {
.button {
padding: 10px;
margin: 0 2px;
font-size: 16px;
width: 45px;
@media screen and (max-width: 768px) {
.cal-outer-body {
width: 350px;
}
}

Expand All @@ -68,7 +70,8 @@ body {

.button:hover {
/* added a button hover effect */
background-color: #00b8b8;
background-color: #000000;
opacity: 0.7;
}

.row {
Expand All @@ -80,44 +83,24 @@ body {
font-size: 20px;
margin: 0;
padding: 10px 38px;
border: 2px solid black;
border-radius: 9px;
}

/* Responsive styles for even smaller screens */
@media (max-width: 300px) {
.row input {
width: 170px;
}
}
@media (max-width: 270px) {
.row input {
width: 130px;
}
border: none;
border-radius: 5px;
}

.cal-outer-body {
border: 2px solid black;
border-radius: 30px;
box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.2);
background-color: #f3ddf2;
margin-left: auto;
display: block;
max-width: 400px;
margin-right: auto;
margin-top: auto;
margin-bottom: auto;
margin: 0 auto;
width: 400px;
box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.2);
background: var(--secondary-color);
border-radius: 10px;
color: #fff;
}

.cal-inner-body {
padding: 10px;
}

.seperator {
/* add a seperator between the input and the buttons */
border: 0.5px solid black;
}

.shake {
animation: shake 0.5s ease-in-out;
}
Expand Down
1 change: 0 additions & 1 deletion utils.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

.flex{
display: flex;

}

.flex-col{
Expand Down

0 comments on commit 866b89c

Please sign in to comment.