-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
57 lines (48 loc) · 1.64 KB
/
app.js
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
const defaultResult = 0;
let currentResult = defaultResult;
let logEntries = [];
// Gets input from input field
function getUserNumberInput() {
return parseInt(userInput.value);
}
// Generates and writes calculation log
function createAndWriteOutput(operator, resultBeforeCalc, calcNumber) {
const calcDescription = `${resultBeforeCalc} ${operator} ${calcNumber}`;
outputResult(currentResult, calcDescription); // from vendor.js file
}
function add() {
const enteredNumber = getUserNumberInput();
const initialResult = currentResult;
currentResult += enteredNumber;
createAndWriteOutput('+', initialResult, enteredNumber);
const logEntry = {
operation: 'ADD',
prevResult: initialResult,
number: enteredNumber,
result: currentResult
};
logEntries.push(logEntry);
console.log(logEntries);
}
function subtract() {
const enteredNumber = getUserNumberInput();
const initialResult = currentResult;
currentResult -= enteredNumber;
createAndWriteOutput('-', initialResult, enteredNumber);
}
function multiply() {
const enteredNumber = getUserNumberInput();
const initialResult = currentResult;
currentResult *= enteredNumber;
createAndWriteOutput('*', initialResult, enteredNumber);
}
function divide() {
const enteredNumber = getUserNumberInput();
const initialResult = currentResult;
currentResult /= enteredNumber;
createAndWriteOutput('/', initialResult, enteredNumber);
}
addBtn.addEventListener('click', add);
subtractBtn.addEventListener('click', subtract);
multiplyBtn.addEventListener('click', multiply);
divideBtn.addEventListener('click', divide);