-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
97 lines (95 loc) · 2.77 KB
/
script.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
'use strict'
const icon = document.querySelector('.icon')
const fas = document.querySelector('.fas');
const body = document.body;
const cantainer = document.getElementById('cantainer');
const display = document.getElementById('display');
const btn = document.getElementsByClassName('btn');
const h1 = document.getElementById('cal');
const input = document.querySelectorAll('input');
let value = '';
const rmwhitetheme = () => {
for (let i = 0; i < btn.length; i++){
btn[i].classList.remove('btn--white');
btn[i].classList.add("btn--dark");
}
h1.style.color = 'white';
}
const rmdarktheme = () => {
for (let i = 0; i < btn.length; i++){
btn[i].classList.remove("btn--dark");
btn[i].classList.add('btn--white');
}
h1.style.color = 'black';
}
const addmoonicon = () => {
fas.classList.remove('fa-sun');
fas.classList.add('fa-moon');
}
const addsunicon = () => {
fas.classList.remove('fa-moon');
fas.classList.add('fa-sun');
}
const displaytheme = (background,color) => {
display.style.background = background;
display.style.color = color;
}
const cantainertheme = (boxShadow, background) => {
cantainer.style.boxShadow = boxShadow;
cantainer.style.backgroundColor = background;
}
const darkmode = () => {
rmwhitetheme();
addsunicon();
body.style.backgroundColor = '#333333';
cantainertheme('0 1.2rem 2rem 0.5rem #262626','#333333');
displaytheme('#ffffff', '#333333');
}
const lightmode = () => {
rmdarktheme();
addmoonicon();
body.style.backgroundColor = '#ffffff';
cantainertheme('0 1.2rem 3rem 0.5rem rgba(0,0,0,0.2)', '#ffffff');
displaytheme('#333333', 'white');
}
const displayvalue = (value) => {
display.value = value;
}
fas.addEventListener("click", function () {
fas.classList.contains('fa-moon') ? darkmode() : lightmode();
})
input.forEach((e) => {
e.addEventListener('click', (event)=>{
if (event.target.value == '=') {
if (value.length != 0) {
let newval = eval(value);
value = ' '+newval;
displayvalue(value);
}
}
else if (event.target.value == 'C') {
value = '';
displayvalue(value);
}
else {
value += event.target.value;
console.log(value);
displayvalue(' '+ value);
}
})
})
document.addEventListener('keydown', function (e) {
if (!isNaN(e.key)) {
value += e.key;
displayvalue(' '+value);
}
else if (e.key ==='-'||e.key==='*'||e.key==='/'||e.key==='+'||e.key===".") {
value += e.key;
displayvalue(' '+value);
}
else if (e.key === 'Enter') {
let newval = eval(value);
value = newval;
displayvalue(' '+value);
}
})