-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
66 lines (57 loc) · 1.6 KB
/
script.js
File metadata and controls
66 lines (57 loc) · 1.6 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
const display = document.getElementById("display");
const historyDisplay = document.getElementById("history-display");
const historyList = document.getElementById("history-list");
let submissions = [];
function addChar(char) {
if (display.value === "0" && "+-/*".includes(char)) {
display.value += char;
}
else if (display.value === "Error" || display.value === "0") {
display.value = char;
}
else {
display.value += char;
}
}
function backspace(){
if (display.value.length > 1 && display.value !== "Error"){
display.value = display.value.slice(0, -1);
} else {
display.value = "0";
}
}
function clearScreen() {
display.value = "0";
}
function addHistory() {
let history = "";
submissions.forEach(element => {
console.log(element);
history += `<div id="history-tab">${element} <button id="history-add" onclick="add('${(element)}')">ADD</button></div>`
});
historyList.innerHTML = history;
}
function calculate() {
var expression = display.value;
try {
display.value = String(eval(expression));
if (submissions.length == 10) {
submissions.pop();
}
submissions.unshift(expression);
addHistory();
}
catch (e) {
display.value = "Error";
}
}
function displayHistory(){
historyDisplay.style.display = 'flex';
}
function hideHistory() {
historyDisplay.style.display = 'none';
}
function add(submission) {
console.log(submission);
display.value = submission;
}