-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
44 lines (43 loc) · 1.41 KB
/
Copy pathscript.js
File metadata and controls
44 lines (43 loc) · 1.41 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
// whrnt he add button is clicked,
// take the value from the first input
// take the value from the second input
// add them together
// udpate the innertext of the result div with the sum.
var firstNumberInput = document.getElementById("firstNumber");
var secondNumberInput = document.getElementById("secondNumber");
function displayResult(res, operation) {
var result = document.getElementById("result");
result.innerText = res;
if (operation == "add") {
result.style.color = "purple";
} else if (operation == "minus") {
result.style.color = "lemonchiffon";
} else if (operation == "mult") {
result.style.color = "palevioletred";
} else {
result.style.color = "coral";
}
}
function add() {
if (!parseInt(firstNumberInput.value) || !parseInt(secondNumberInput.value)) {
displayResult("ERROR", "na");
} else {
var sum =
parseInt(firstNumberInput.value) + parseInt(secondNumberInput.value);
displayResult(sum, "add");
}
}
function subtract() {
var diff =
parseInt(firstNumberInput.value) - parseInt(secondNumberInput.value);
displayResult(diff, "minus");
}
function multiply() {
var product =
parseInt(firstNumberInput.value) * parseInt(secondNumberInput.value);
displayResult(product, "mult");
}
function divide() {
var q = parseInt(firstNumberInput.value) / parseInt(secondNumberInput.value);
displayResult(q, "div");
}