-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
159 lines (141 loc) · 4.63 KB
/
Copy pathindex.html
File metadata and controls
159 lines (141 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Sum Calculator</title>
<style>
body {
font-family: 'Roboto', sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background: linear-gradient(135deg, #ffcc70, #ff6f69);
margin: 0;
color: #333;
}
.container {
background: white;
width: 350px;
padding: 50px;
border-radius: 12px;
box-shadow: 0 8px 15px rgba(0, 0, 0, 0.1);
text-align: center;
}
h1 {
font-size: 28px;
color: #444;
margin-bottom: 20px;
text-transform: uppercase;
letter-spacing: 1.5px;
}
input {
width: 80%;
padding: 15px;
margin: 10px 0;
border: 1px solid #ddd;
border-radius: 8px;
font-size: 16px;
text-align: center;
outline: none;
transition: border-color 0.2s ease-in-out;
}
input:focus {
border-color: #007bff;
}
button {
padding: 15px 30px;
margin: 20px 0;
background-color: #007bff;
color: white;
border: none;
border-radius: 8px;
font-size: 18px;
cursor: pointer;
transition: transform 0.1s ease-in-out, background-color 0.1s ease;
}
button:hover {
background-color: #0056b3;
}
button:active {
transform: scale(1.05);
}
#result {
font-size: 22px;
margin: 0px auto;
padding: 15px;
border: 1px solid #ccc;
border-radius: 8px;
width: 90%;
text-align: center;
background-color: #e9ecef;
transition: background-color 0.25s ease-in-out, transform 0.2s ease-in-out;
}
</style>
</head>
<body>
<div class="container">
<h1>Add Two Numbers</h1>
<input id="num1" placeholder="Enter first number">
<input id="num2" placeholder="Enter second number">
<button onclick="calculateSum()">Calculate Sum</button>
<div id="result">Result: </div>
</div>
<script>
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
async function calculateSum() {
var num1 = document.getElementById('num1').value;
var num2 = document.getElementById('num2').value;
try {
const response = await fetch('http://localhost:3000/calculate', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ num1, num2 })
});
const data = await response.json();
var result = document.getElementById('result');
result.innerText = 'Result: ' + data.sum;
result.style.backgroundColor = getRandomColor();
result.style.transform = 'scale(1.1)';
setTimeout(function () {
result.style.transform = 'scale(1)';
}, 200);
document.getElementById('num1').focus();
document.getElementById('num1').value = '';
document.getElementById('num2').value = '';
} catch (err) {
console.log(err);
}
}
function handleEnterKey(event, nextElementId) {
if (event.key === "Enter") {
if (nextElementId) {
document.getElementById(nextElementId).focus();
} else {
calculateSum();
}
}
}
document.getElementById('num1').addEventListener('keydown', function (event) {
handleEnterKey(event, 'num2');
});
document.getElementById('num2').addEventListener('keydown', function (event) {
handleEnterKey(event, null);
});
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('num1').focus();
});
</script>
</body>
</html>