-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexemplo.js
More file actions
69 lines (58 loc) · 1.44 KB
/
Copy pathexemplo.js
File metadata and controls
69 lines (58 loc) · 1.44 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
let op = function() {
alert("Escolha uma opção antes");
}
function sum(num1, num2) {
return num1 + num2;
}
function sub(num1, num2) {
return num1 - num2;
}
function div(num1, num2) {
if(num1==0){
return 0;
}
if (num2 == 0){
return "Não é possível dividir por zero";
}else{
return num1 / num2;
}
}
function mult(num1, num2) {
return num1 * num2;
}
function operation() {
const answer = document.getElementById('answer');
const select = document.getElementById('operation');
const selectedOperation = select.selectedIndex;
switch (selectedOperation) {
case 1:
answer.innerText = '';
op = sum;
break;
case 2:
answer.innerText = '';
op = sub;
break;
default:
break;
case 3:
answer.innerText = '';
op = div;
break;
case 4:
answer.innerText = ' ';
op = mult;
break;
}
}
function calculate() {
// referências aos elementos
const num1Input = document.getElementById('num1');
const num2Input = document.getElementById('num2');
const answer = document.getElementById('answer');
// valores dos inputs
const num1 = parseInt(num1Input.value);
const num2 = parseInt(num2Input.value);
const resp = op(num1, num2);
answer.innerText = resp;
}