Skip to content

Commit

Permalink
using eval function
Browse files Browse the repository at this point in the history
  • Loading branch information
arrehman3 committed Jul 8, 2024
1 parent 6a25154 commit 33e1415
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
33 changes: 33 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
<link rel="stylesheet" href="style.css">
<script src="main.js" defer></script>
</head>
<body>
<div class="maincontainer">
<div class="screen">Screen</div>
<div class="buttons">
<div>0</div>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>+</div>
<div>-</div>
<div>*</div>
<div>/</div>
<div>CE</div>
<div>=</div>
</div>
</div>
</body>
</html>
28 changes: 28 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
document.addEventListener('DOMContentLoaded',function(){
const screen = document.querySelector('.screen');
const buttons = document.querySelectorAll('.buttons div');
let currentInput='';
let result='';

buttons.forEach(button=>{
button.addEventListener("click",()=>{
const value = button.textContent;

if (value === "CE") {
currentInput = "";
result = "";
} else if (value === "=") {
try {
result = eval(currentInput);
} catch {
result = "Error";
}
currentInput = result;
} else {
currentInput += value;
}

screen.textContent = currentInput || result || "0";
})
})
})
55 changes: 55 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
margin: 0;
font-family: Arial, sans-serif;
}

.maincontainer{
width: 340px;
height: auto;
border: 1px solid #100e0e;
background-color: #fff;
}
.screen{
display: flex;
align-items: center;
justify-content: center;
border: 1px solid #100e0e;
height: 50px;
background-color: #222;
padding: 0 10px;
border-top-left-radius: 10px;
border-top-right-radius: 10px;
color: #fff;
}
.buttons{
display: grid;
grid-template-columns: repeat(4,1fr);
gap: 10px;
padding: 10px;
}

.buttons div{
height: 50px;
background-color: #ddd;
display: flex;
justify-content: center;
align-items: center;
font-size: 1.5rem;
border:1.5px solid bisque;
cursor: pointer;
}

.buttons div:hover{
background-color: #999;
}

.buttons div:active{
background-color: #3f3f3f;
}

29 changes: 29 additions & 0 deletions test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Center Div with Flex</title>
<style>
.parent {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
border: 1px solid #000;
}
.child {
/* width: 50%;
height: 50%;
background-color: lightblue; */
}
</style>
</head>
<body>
<div class="parent">
<div class="child">
Centered Child
</div>
</div>
</body>
</html>

0 comments on commit 33e1415

Please sign in to comment.