-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathaula05.01.html
More file actions
85 lines (61 loc) · 2.52 KB
/
aula05.01.html
File metadata and controls
85 lines (61 loc) · 2.52 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
<!doctype html>
<html>
<head>
<title>Logica de Programação Javascript</title>
<meta charset="utf-8">
</head>
<body>
<h1>Lógica de programação com Javascript</h1>
<button onclick="iniciarCalculo('+')">+</button>
<button onclick="iniciarCalculo('-')">-</button>
<button onclick="iniciarCalculo('*')">*</button>
<button onclick="iniciarCalculo('/')">/</button>
<div id="output"></div>
<script>
var n2 = prompt('digite um numero');
n2 = parseFloat(n2);
escreve(n2);
function iniciarCalculo(simbolo){
var n1 = document.querySelector('#output').textContent;
n1 = parseFloat(n1);
var n2 = prompt('digite outro número');
n2 = parseFloat(n2);
try{
var msg = calcular(simbolo, n1, n2);
} catch(e){
alert(e);
return;
}
escreve(msg);
}
function escreve(mensagem){
var output = document.querySelector('#output');
output.innerHTML = mensagem;
}
function calcular(simbolo, n1, n2){
console.log(simbolo)
if(simbolo !== '+' && simbolo !== '-' && simbolo !== '*' && simbolo !== '/'){
throw new Error('digite um simbolo valido')
}
if( isNaN(n1) || isNaN(n2) ){
throw new Error('chama passando somente numeros')
}
var numeroCalculado = null;
switch(simbolo){
case '+':
numeroCalculado = n1 + n2;
break;
case '-':
numeroCalculado = n1 - n2;
break;
case '*':
numeroCalculado = n1 * n2;
break;
case '/':
numeroCalculado = n1 / n2;
}
return numeroCalculado;
}
</script>
</body>
</html>