forked from codemistic/Web-Development
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
90 lines (81 loc) · 2.19 KB
/
script.js
File metadata and controls
90 lines (81 loc) · 2.19 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
let lightTheme = "styles/light.css";
let darkTheme = "styles/dark.css";
//adding event handler on the document to handle keyboard inputs
document.addEventListener("keydown", keyboardInputHandler);
//function to handle keyboard inputs
function keyboardInputHandler(e) {
//grabbing the liveScreen
let res = document.getElementById("result");
//numbers
if (e.key === "0") {
res.value += "0";
} else if (e.key === "1") {
res.value += "1";
} else if (e.key === "2") {
res.value += "2";
} else if (e.key === "3") {
res.value += "3";
} else if (e.key === "4") {
res.value += "4";
} else if (e.key === "5") {
res.value += "5";
} else if (e.key === "6") {
res.value += "6";
} else if (e.key === "7") {
res.value += "7";
} else if (e.key === "7") {
res.value += "7";
} else if (e.key === "8") {
res.value += "8";
} else if (e.key === "9") {
res.value += "9";
}
//operators
if (e.key === "+") {
res.value += "+";
} else if (e.key === "-") {
res.value += "-";
} else if (e.key === "*") {
res.value += "*";
} else if (e.key === "/") {
res.value += "/";
}
//decimal key
if (e.key === ".") {
res.value += ".";
}
//press enter to see result
if (e.key === "Enter") {
res.value = eval(result.value || null);
}
//backspace for removing the last input
if (e.key === "Backspace") {
let resultInput = res.value;
//remove the last element in the string
res.value = resultInput.substring(0, res.value.length - 1);
}
}
// Clears the screen on click of C button.
function clearScreen() {
document.getElementById("result").value = "";
}
// Displays entered value on screen.
function liveScreen(value) {
let res = document.getElementById("result");
if (!res.value) {
res.value = "";
}
res.value += value;
}
// Swaps the stylesheet in order to achieve dark mode.
function changeTheme() {
let darkMode = document.getElementById("dark-mode");
let theme = document.getElementById("theme");
if (theme.getAttribute("href") === lightTheme) {
theme.href = darkTheme;
darkMode.innerHTML = "Light Mode 🌞";
} else {
theme.href = lightTheme;
darkMode.innerHTML = "Dark Mode 🌙";
}
}