Skip to content

Commit 50319ac

Browse files
committed
add logic for decimal
1 parent 31ea0ad commit 50319ac

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

calculator.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,10 @@
9393
inputField.stringToEval += inputField.currentValue;
9494

9595
// Append number to string to make a multi digit number
96-
} else if (numbers.has(inputField.previousValue)) {
96+
} else if (numbers.has(inputField.previousValue) || decimal.has(inputField.previousValue)) {
9797
// Append the number clicked to the display
9898
inputField.display += inputField.currentValue;
99+
inputField.stringToEval += inputField.currentValue;
99100
}
100101
} else if(operators.has(inputField.currentValue)) {
101102
// - **Operators**: if a user clicks on an operator:
@@ -116,6 +117,12 @@
116117
// - append a decimal to string
117118
// - but only if the previous value is a number
118119
// - and not if there is already a decimal in the string
120+
if( (numbers.has(inputField.previousValue) || equals.has(inputField.previousValue)) && !inputField.display.includes('.')) {
121+
// Append the decimal to the display
122+
inputField.display += inputField.currentValue;
123+
// Append the decimal to the string to evaluate
124+
inputField.stringToEval += inputField.currentValue;
125+
}
119126

120127
} else if(clear.has(inputField.currentValue)) {
121128
// - **Clear**: if a user clicks on clear:
@@ -144,7 +151,7 @@
144151
display.textContent = inputField.display;
145152

146153
// Print to console for debugging
147-
console.log(`Before button logic: ${JSON.stringify(inputField)}`);
154+
console.log(`After button logic: ${JSON.stringify(inputField)}`);
148155
});
149156
});
150157
});

0 commit comments

Comments
 (0)