Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the validation for 0 and '+' operator #1179

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Calculator/yash_pokharna/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!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="styles.css">
</head>
<body>
<div class="calculator">
<input type="text" id="display" readonly>
<div class="buttons">
<button onclick="clearDisplay()">C</button>
<button onclick="addOperator('/')">/</button>
<button onclick="addOperator('*')">*</button>
<button onclick="deleteLast()">DEL</button><br>
<button onclick="appendNumber('7')">7</button>
<button onclick="appendNumber('8')">8</button>
<button onclick="appendNumber('9')">9</button>
<button onclick="addOperator('-')">-</button><br>
<button onclick="appendNumber('4')">4</button>
<button onclick="appendNumber('5')">5</button>
<button onclick="appendNumber('6')">6</button>
<button onclick="addOperator('+')">+</button><br>
<button onclick="appendNumber('1')">1</button>
<button onclick="appendNumber('2')">2</button>
<button onclick="appendNumber('3')">3</button>
<button onclick="calculate()">=</button><br>
<button onclick="appendNumber('0')">0</button>
<button onclick="appendNumber('.')">.</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
38 changes: 38 additions & 0 deletions Calculator/yash_pokharna/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
let display = document.getElementById("display");

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

function deleteLast() {
display.value = display.value.slice(0, -1);
}

function appendNumber(number) {
if (number === '.' && display.value.includes('.') && !isNaN(display.value.slice(-1))) {
alert("Error: Multiple decimal points are not allowed.");
return;
}
display.value += number;
}

function addOperator(operator) {
const lastChar = display.value.slice(-1);
if (!display.value || isNaN(lastChar)) {
alert("Error: Cannot add consecutive operators.");
return;
}
display.value += operator;
}

function calculate() {
try {
if (display.value.includes("/0")) {
alert("Error: Division by zero is undefined.");
return;
}
display.value = eval(display.value);
} catch (error) {
alert("Error: Invalid operation.");
}
}
52 changes: 52 additions & 0 deletions Calculator/yash_pokharna/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f5f5f5;
}

.calculator {
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
}

#display {
width: 100%;
height: 50px;
font-size: 18px;
margin-bottom: 10px;
text-align: right;
padding: 5px;
border: 1px solid #ccc;
border-radius: 5px;
background: #f9f9f9;
}

.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}

button {
height: 50px;
font-size: 18px;
border: none;
border-radius: 5px;
cursor: pointer;
background: #007BFF;
color: white;
}

button:hover {
background: #0056b3;
}

button:active {
background: #003f7f;
}