Al Rouman (Web Lab Report 2)
Al Rouman (Web Lab Report 2)
Name ID
4. IMPLEMENTATION
HTML:
<!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">
</head>
<body>
<div class="calculator">
<input type="text" id="display" disabled>
<div class="buttons">
<button onclick="clearDisplay()">C</button>
<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('4')">4</button>
<button onclick="appendToDisplay('5')">5</button>
<button onclick="appendToDisplay('6')">6</button>
<button onclick="appendToDisplay('*')">×</button>
<button onclick="appendToDisplay('1')">1</button>
<button onclick="appendToDisplay('2')">2</button>
<button onclick="appendToDisplay('3')">3</button>
<button onclick="appendToDisplay('-')">-</button>
<button onclick="appendToDisplay('0')">0</button>
<button onclick="appendToDisplay('.')">.</button>
<button onclick="calculate()">=</button>
<button onclick="appendToDisplay('+')">+</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS:
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
.calculator {
width: 200px;
background-color: #cfcfcf;
padding: 10px;
border-radius: 8px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
}
#display {
width: 100%;
height: 40px;
text-align: right;
font-size: 24px;
margin-bottom: 10px;
padding: 5px;
border: none;
background-color: #e0e0e0;
border-radius: 4px;
}
.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 5px;
}
button {
height: 40px;
font-size: 18px;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:active {
background-color: #b0b0b0;
}
button:nth-child(1) {
background-color: #ff6666;
}
button:nth-child(5n) {
background-color: #66cc66;
}
JavaScript:
function appendToDisplay(value) {
document.getElementById("display").value += value;
}
function clearDisplay() {
document.getElementById("display").value = "";
}
function calculate() {
try {
document.getElementById("display").value = eval(document.getElementById("display").value);
} catch (error) {
document.getElementById("display").value = "Error";
}
}
Output:
6. ANALYSIS AND DISCUSSION
The calculator project combines HTML for structure, CSS for styling, and JavaScript for
functionality, creating a simple yet effective web calculator. HTML forms the interface with
input fields and buttons, CSS enhances visual design, and JavaScript manages operations
like addition, subtraction, multiplication, and division. Each button triggers JavaScript
functions, enabling real-time updates on the display, and CSS provides a clean, user-friendly
layout. While functional, the project could benefit from improvements such as custom
parsing to replace eval() for security, better error handling, advanced features (like
percentages and square roots), and responsive design for mobile.
7. SUMMARY:
The calculator project uses HTML, CSS, and JavaScript to create a basic, functional web calculator.
HTML defines the structure with input fields and buttons, CSS enhances the design, and JavaScript
controls the calculations. While the calculator works well, improvements like custom parsing for
security, advanced features, responsive design, and better error handling could make it more
versatile and user-friendly. This project effectively demonstrates essential web development skills
and provides a strong foundation for further enhancements.