|
if(numbers.has(inputField.currentValue)) { |
|
// Starting a new calculation |
|
if(inputField.display === ' ' || equals.has(inputField.previousValue)) { |
|
// Update display with the number clicked |
|
inputField.display = inputField.currentValue; |
|
// Update the string to evaluate |
|
inputField.stringToEval = inputField.currentValue; |
|
|
|
// Appending a number to a current calculation, when previous button is an operator |
|
} else if (operators.has(inputField.previousValue)) { |
|
// Update the display with current number |
|
inputField.display = inputField.currentValue; |
|
// Update the string to evaluate |
|
inputField.stringToEval += inputField.currentValue; |
|
|
|
// Append number to string to make a multi digit number |
|
} else if (numbers.has(inputField.previousValue) || decimal.has(inputField.previousValue)) { |
|
// Append the number clicked to the display |
|
inputField.display += inputField.currentValue; |
|
inputField.stringToEval += inputField.currentValue; |
|
} |
|
} else if(operators.has(inputField.currentValue)) { |
|
// - **Operators**: if a user clicks on an operator: |
|
// - append that operator to the string to evaluate only when the previous value is a number (not a decimal) |
|
// - however the display should not change, the previous number should stay the same at this point |
|
// - if the user clicks on the operator multiple times, ie ++++_*/ one after another then replace the last character (an operator character) with the operator that was last clicked. |
|
|
|
if(operators.has(inputField.previousValue)) { |
|
// Replace the last operator with the current operator |
|
inputField.stringToEval = inputField.stringToEval.slice(0, -1) + inputField.currentValue; |
|
} else if(numbers.has(inputField.previousValue) || equals.has(inputField.previousValue)) { |
|
// Append the operator to the string to evaluate |
|
inputField.stringToEval += inputField.currentValue; |
|
} |
|
|
|
} else if(decimal.has(inputField.currentValue)) { |
|
// - **Decimal**: if a user clicks on decimal: |
|
// - append a decimal to string |
|
// - but only if the previous value is a number |
|
// - and not if there is already a decimal in the string |
|
if( (numbers.has(inputField.previousValue) || equals.has(inputField.previousValue)) && !inputField.display.includes('.')) { |
|
// Append the decimal to the display |
|
inputField.display += inputField.currentValue; |
|
// Append the decimal to the string to evaluate |
|
inputField.stringToEval += inputField.currentValue; |
|
} |
|
|
|
} else if(clear.has(inputField.currentValue)) { |
|
// - **Clear**: if a user clicks on clear: |
|
// - replace the display with "0" |
|
// - replace the string to evaluate with null |
|
inputField.display = ' '; |
|
inputField.stringToEval = null; |
|
|
|
} else if(equals.has(inputField.currentValue)) { |
|
// - **Equals**: if a user clicks on equals: |
|
// - evalulate that string |
|
// - but only evaluate if the last character is a number, it cannot be a "." or an operator character. |
|
// - replace the display with the result of the evaluation |
|
if (numbers.has(inputField.previousValue)) { |
|
// Evaluate the string |
|
inputField.display = eval(inputField.stringToEval).toString(); |
|
// Update the string to evaluate |
|
inputField.stringToEval = inputField.display; |
javaScriptCalculator/calculator.html
Lines 80 to 143 in 50319ac
This code is not easy to read and not easy to maintain.
Improve by making helper functions and using switch cases.