-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart1_6.html
More file actions
94 lines (88 loc) · 3.04 KB
/
Copy pathpart1_6.html
File metadata and controls
94 lines (88 loc) · 3.04 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Number Analysis Tool</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: grey;
color: white;
text-align: center;
padding: 20px;
}
#numberForm {
margin-bottom: 20px;
}
input, button {
padding: 10px;
margin: 5px;
border-radius: 5px;
border: none;
}
button {
background-color: pink;
cursor: pointer;
}
button:hover {
background-color: #ff69b4;
}
#result {
margin-top: 20px;
border: 1px solid white;
padding: 10px;
}
a {
color: white;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>Number Analysis Tool</h1>
<form id="numberForm">
<label for="numberInput">Enter a number with at least 4 decimal places:</label><br>
<input type="text" id="numberInput" required>
<button type="submit">Submit</button>
<button type="button" onclick="clearForm()">Clear</button>
</form>
<div id="result"></div>
<a href="homework6.html">Back to Main Page of Homwork 6</a>
<script>
document.getElementById('numberForm').addEventListener('submit', function(event) {
event.preventDefault();
const input = document.getElementById('numberInput').value;
processNumber(input);
});
function processNumber(numberStr) {
const resultElement = document.getElementById('result');
const number = parseFloat(numberStr);
if (!numberStr.match(/^\d+\.\d{4,}$/)) {
resultElement.textContent = "You need to type a number with at least 4 decimals, please try again";
return;
}
const roundedInteger = Math.round(number);
const sqrtRounded = Math.round(Math.sqrt(number));
const roundedTenth = parseFloat(number.toFixed(1));
const roundedHundredth = parseFloat(number.toFixed(2));
const roundedThousandth = parseFloat(number.toFixed(3));
resultElement.innerHTML = `
You typed number ${number}<br>
Rounded to the nearest integer = ${roundedInteger}<br>
Square root rounded to integer = ${sqrtRounded}<br>
Rounded to the nearest 10th position = ${roundedTenth}<br>
Rounded to the nearest 100th position = ${roundedHundredth}<br>
Rounded to the nearest 1000th position = ${roundedThousandth}
`;
}
function clearForm() {
document.getElementById('numberInput').value = '';
document.getElementById('result').textContent = '';
}
</script>
</body>
</html>