-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.js
78 lines (76 loc) · 2.1 KB
/
calc.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
document.addEventListener('DOMContentLoaded', reset)
document.addEventListener("click", clicked);
var display;
var displayContent;
var listNumbers;
var listOperators;
var calculatedNumber;
function reset () {
display = document.getElementById("display")
displayContent = "";
listNumbers = [];
listOperators = [];
updateDisplay();
}
function clicked(event){
var buttonClicked = event.target;
if (buttonClicked.classList.contains("number")){
displayContent += buttonClicked.innerHTML;}
else if (buttonClicked.classList.contains("operator")) {
addCalculation(buttonClicked.id);}
else{
switch(buttonClicked.id) {
case "decimal":
if (!displayContent.includes(".")) displayContent += ".";
break;
case "clear":
reset();
break;
case "clearentry":
displayContent = "";
case "equals":
listNumbers.push(convertToNumber(display.value));
displayContent = doCalculations();
break; }
}
updateDisplay();
}
function addCalculation (operator) {
listNumbers.push(convertToNumber(display.value));
listOperators.push(operator);
displayContent = "";
updateDisplay();
}
function doCalculations () {
console.log(listNumbers);
console.log(listOperators);
let num1 = listNumbers.splice(0,1);
let num2 = listNumbers.splice(0,1);
let operator = listOperators.splice(0,1);
console.log(num1+" "+operator+" "+num2)
let result = doOperation(num1, num2, operator);
if (listOperators.length==0) {
return result;}
else {
listNumbers.unshift(result);
return doCalculations ();}
}
function doOperation (num1, num2, operator) {
if (operator=="add") {
return Number(num1)+Number(num2);}
else if (operator=="subtract") {
return num1 - num2; }
else if (operator=="multiply") {
return num1 * num2; }
else if (operator=="divide") {
return num1/num2; }
}
function convertToNumber(n) {
let int = parseInt(n)
if (isNaN(int)) {
return 0;}
return int;
}
function updateDisplay () {
display.value = displayContent;
}