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
160
161
162
163
164
165
166
167
168
169
170
171
|
<!DOCTYPE html>
<html>
<head lang="pt">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
<style type="text/css" media="screen">
/* Resettings some html properties */
html, body, div, h1, header,section{
margin: 0;
padding: 0;
border: 0;
}
h1 {
font-size: 1.25rem;
color: #fff;
text-transform: uppercase;
}
ul {
margin: 0;
}
body {
font-family: sans-serif;
background-color: #f4f4f4;
}
header {
position: relative;
margin: auto;
margin-bottom: 20px;
background: #0062cc;
padding: 10px;
max-width: 900px;
}
nav {
position: absolute;
top: 10px;
right: 10px;
}
.warning {
font-size: small;
color: red;
visibility: hidden;
}
.btn {
display: inline-block;
padding: .1rem .75rem;
background: #e9ecef;
border: #343a40 1px solid;
font-size: .9rem;
font-weight: 400;
line-height: 1.5;
cursor: pointer;
color: #000;
border-radius: 0;
}
nav li {
display: inline;
margin: 0 0 0 0;
}
.section {
justify-content: center;
margin-bottom: 20px;
display: flex;
}
.form-ctl {
border-radius: 0;
border-color: #888;
color: #343a40;
padding: .25rem .375rem;
}
main {
width: 50%;
margin: 0 auto;
}
</style>
</head>
<body>
<header>
<h1>Regra de três</h1>
<nav>
<ul>
<li>
<a class="btn" href="https://git.sr.ht/~gabrielgio/tres">Código fonte</a>
</li>
</ul>
</nav>
</header>
<main>
<div class="section">
<input class="form-ctl" type="number" id="a" placeholder="A">
=
<input class="form-ctl" type="number" id="b" placeholder="B">
</div>
<div class="section">
<input class="form-ctl" type="number" id="c" placeholder="C">
=
<input class="form-ctl" type="number" id="resultado" disabled placeholder="Resultado">
</div>
<div id="warning" class="section warning">
<p>
</p>
</div>
<div class="section">
<input type="button" class="btn" value="Calcular" onclick="eval()">
</div>
</main>
<script>
const aInput = document.getElementById('a')
const bInput = document.getElementById('b')
const cInput = document.getElementById('c')
const resultadoInput = document.getElementById('resultado')
const warningParagraph = document.getElementById('warning')
function eval() {
const a = aInput.value
const b = bInput.value
const c = cInput.value
try {
const values = validate(a,b,c)
resultadoInput.value = calculate(...values)
} catch (e) {
warningParagraph.textContent = e
warningParagraph.style.visibility = 'visible';
setTimeout(() => {
warningParagraph.style.visibility = 'hidden';
}, 3000)
}
}
function validate(a, b, c) {
const result = [0,0,0]
const ra = parseInt(a)
if (isNaN(ra)){
throw "Valor de A invalido"
}
result[0] = ra
const rb = parseInt(b)
if (isNaN(rb)){
throw "Valor de B invalido"
}
result[1] = rb
const rc = parseInt(c)
if (isNaN(rc)){
throw "Valor de C invalido"
}
result[2] = rc
return result
}
function calculate(a, b, c) {
return (b*c)/a
}
</script>
</body>
</html>
|