- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp2.js
More file actions
Latest commit
70 lines (61 loc) · 2.12 KB
/
Copy pathapp2.js
File metadata and controls
70 lines (61 loc) · 2.12 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
// Rewrite of JS functionality - Dom manipulation
// Basic storage variables and functions for calculator
constcalculator={
memory: 0,
storedFunction: "",
screen: "",
add(val){this.memory+=val;},
subtract(val){this.memory-=val;},
times(val){this.memory=this.memory*val;},
divide(val){this.memory=this.memory/val;},
}
// DOM Elements used:
constnumberButtons=document.querySelectorAll(".number");
constfunctionButtons=document.querySelectorAll(".function");
constcalcScreen=document.getElementById('entry');
// Write out function
constwriteToScreen=()=>{
calcScreen.textContent=calculator.screen;
}
// Adding functionality to number buttons
// on click concat number to screen variable
// write out to screen
numberButtons.forEach((el)=>{
el.addEventListener('click',
()=>{
calculator.screen+=el.textContent;
writeToScreen();
});
});
// Adding functionality to function keys
// if clear button, clear all variables
// if equals button, run memory number (1) with function and number on screen (2), store result in memory
// else store number and function
functionButtons.forEach((el)=>{
el.addEventListener('click',()=>{
if(el.id=="clear"){
calculator.memory=0;
calculator.storedFunction="";
calculator.screen="";
}elseif(el.id=="equals"){
letmethod=calculator.storedFunction;
calculator[method](parseInt(calculator.screen));
calculator.screen=calculator.memory;
}else{
calculator.memory=parseInt(calculator.screen);
calculator.storedFunction=el.id;
calculator.screen="";
}
writeToScreen();
});
});
// Things to do:
// add more buttons
// decimal point
// square button
// Back button?
// add compound calculations
// more memory slots
// add handler in print function for long inputs / rounding
// add page embellishments
// add keystroke option i.e typed input