Skip to content

Commit b608b77

Browse files
authored
added the keypress functionality
i added the keypress funcitonality which allows the control through user keyboard
1 parent d1fd952 commit b608b77

File tree

1 file changed

+108
-43
lines changed

1 file changed

+108
-43
lines changed

Calculator/sushil-2803/use.js

Lines changed: 108 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,57 +8,109 @@ var input = document.getElementById('input'), // input/output button
88
resultDisplayed = false; // flag to keep an eye on what output is displayed
99

1010
// adding click handlers to number buttons
11-
for (var i = 0; i < number.length; i++) {
12-
number[i].addEventListener("click", function(e) {
1311

14-
// storing current input string and its last character in variables - used later
15-
var currentString = input.innerHTML;
16-
var lastChar = currentString[currentString.length - 1];
17-
18-
// if result is not diplayed, just keep adding
19-
if (resultDisplayed === false) {
20-
input.innerHTML += e.target.innerHTML;
21-
} else if (resultDisplayed === true && lastChar === "+" || lastChar === "-" || lastChar === "×" || lastChar === "÷") {
22-
// if result is currently displayed and user pressed an operator
23-
// we need to keep on adding to the string for next operation
24-
resultDisplayed = false;
25-
input.innerHTML += e.target.innerHTML;
26-
} else {
27-
// if result is currently displayed and user pressed a number
28-
// we need clear the input string and add the new input to start the new opration
29-
resultDisplayed = false;
30-
input.innerHTML = "";
31-
input.innerHTML += e.target.innerHTML;
32-
}
33-
34-
});
12+
function number_handler(event) {
13+
// storing current input string and its last character in variables - used later
14+
var currentString = input.innerHTML;
15+
var lastChar = currentString[currentString.length - 1];
16+
17+
// if result is not diplayed, just keep adding
18+
if (resultDisplayed === false) {
19+
input.innerHTML += event.target.innerHTML;
20+
} else if (resultDisplayed === true && lastChar === "+" || lastChar === "-" || lastChar === "×" || lastChar === "÷") {
21+
// if result is currently displayed and user pressed an operator
22+
// we need to keep on adding to the string for next operation
23+
resultDisplayed = false;
24+
input.innerHTML += event.target.innerHTML;
25+
} else {
26+
// if result is currently displayed and user pressed a number
27+
// we need clear the input string and add the new input to start the new opration
28+
resultDisplayed = false;
29+
input.innerHTML = "";
30+
input.innerHTML += event.target.innerHTML;
31+
}
32+
33+
}
34+
for (var i = 0; i < number.length; i++) {
35+
number[i].addEventListener("click",number_handler );
3536
}
3637

3738
// adding click handlers to number buttons
39+
function operator_handler(event) {
40+
// storing current input string and its last character in variables - used later
41+
var currentString = input.innerHTML;
42+
var lastChar = currentString[currentString.length - 1];
43+
44+
// if last character entered is an operator, replace it with the currently pressed one
45+
if (lastChar === "+" || lastChar === "-" || lastChar === "x" || lastChar === "÷") {
46+
var newString = currentString.substring(0, currentString.length - 1) + event.target.innerHTML;
47+
input.innerHTML = newString;
48+
} else if (currentString.length == 0) {
49+
// if first key pressed is an opearator, don't do anything
50+
console.log("enter a number first");
51+
} else {
52+
// else just add the operator pressed to the input
53+
input.innerHTML += event.target.innerHTML;
54+
}
55+
56+
}
3857
for (var i = 0; i < operator.length; i++) {
39-
operator[i].addEventListener("click", function(e) {
58+
operator[i].addEventListener("click",operator_handler );
59+
}
4060

61+
// adding a window eventListener to add keypress events to window
62+
function windows_handler(event){
63+
if (parseInt(event.key)<=9 || parseInt(event.key)>=0) {
4164
// storing current input string and its last character in variables - used later
42-
var currentString = input.innerHTML;
43-
var lastChar = currentString[currentString.length - 1];
44-
45-
// if last character entered is an operator, replace it with the currently pressed one
46-
if (lastChar === "+" || lastChar === "-" || lastChar === "×" || lastChar === "÷") {
47-
var newString = currentString.substring(0, currentString.length - 1) + e.target.innerHTML;
48-
input.innerHTML = newString;
49-
} else if (currentString.length == 0) {
50-
// if first key pressed is an opearator, don't do anything
51-
console.log("enter a number first");
52-
} else {
53-
// else just add the operator pressed to the input
54-
input.innerHTML += e.target.innerHTML;
55-
}
56-
57-
});
65+
var currentString = input.innerHTML;
66+
var lastChar = currentString[currentString.length - 1];
67+
68+
// if result is not diplayed, just keep adding
69+
if (resultDisplayed === false) {
70+
input.innerHTML += parseInt(event.key);
71+
} else if (resultDisplayed === true && lastChar === "+" || lastChar === "-" || lastChar === "×" || lastChar === "÷") {
72+
// if result is currently displayed and user pressed an operator
73+
// we need to keep on adding to the string for next operation
74+
resultDisplayed = false;
75+
input.innerHTML += parseInt(event.key);
76+
} else {
77+
// if result is currently displayed and user pressed a number
78+
// we need clear the input string and add the new input to start the new opration
79+
resultDisplayed = false;
80+
input.innerHTML = "";
81+
input.innerHTML += parseInt(event.key);
82+
}
83+
}
84+
else if(event.key === "+" || event.key === "-" || event.key === "x" || event.key === "÷" || event.key === "/" || event.key === "*" ){
85+
// storing current input string and its last character in variables - used later
86+
var currentString = input.innerHTML;
87+
var lastChar = currentString[currentString.length - 1];
88+
var s=event.key;
89+
if(s=="/"){ // deckared a variable s to accomodte / as ÷
90+
s="÷"
91+
}
92+
if(s=="*"){ // to accomodate * as ×
93+
s=document.querySelector('.multiply_sign').textContent; // to apply sign x when * is pressed
94+
}
95+
// if last character entered is an operator, replace it with the currently pressed one
96+
if (lastChar === "+" || lastChar === "-" || lastChar === "x" || lastChar === "÷") {
97+
var newString = currentString.substring(0, currentString.length - 1) + s;
98+
input.innerHTML = newString;
99+
} else if (currentString.length == 0) {
100+
// if first key pressed is an opearator, don't do anything
101+
console.log("enter a number first");
102+
} else {
103+
// else just add the operator pressed to the input
104+
input.innerHTML += s;
105+
}
106+
}
58107
}
108+
window.addEventListener("keypress",windows_handler);
109+
59110

60111
// on click of 'equal' button
61-
result.addEventListener("click", function() {
112+
113+
function output(){
62114

63115
// this is the string that we will be processing eg. -10+26+33-56*34/23
64116
var inputString = input.innerHTML;
@@ -112,9 +164,22 @@ result.addEventListener("click", function() {
112164
input.innerHTML = numbers[0]; // displaying the output
113165

114166
resultDisplayed = true; // turning flag if result is displayed
115-
});
167+
}
168+
result.addEventListener("click",output );
169+
116170

117171
// clearing the input on press of clear
118172
clear.addEventListener("click", function() {
119173
input.innerHTML = "";
120-
})
174+
})
175+
176+
//adding a event for clearing with the help of backspace
177+
window.onkeydown = function(event){
178+
let key = event.key;
179+
if (key === "Backspace") {
180+
input.innerHTML = "";
181+
}
182+
else if(key==="Enter"){
183+
output()
184+
}
185+
}

0 commit comments

Comments
 (0)