108 lines
3.9 KiB
JavaScript
108 lines
3.9 KiB
JavaScript
// Creates the Calculator Object
|
|
function Calculator() {
|
|
// Set current and buffer values to 0
|
|
this.current = 0;
|
|
this.buffer = "0";
|
|
// Set previous operator to null
|
|
this.previousOperator = null;
|
|
// When button is clicked, if not a number, buffer an operator
|
|
// if a number, buffer the operand.
|
|
this.buttonClicked = ( value ) => {
|
|
if (isNaN(parseInt(value))) {
|
|
calculator.bufferOperator(value);
|
|
} else {
|
|
calculator.bufferOperand(value);
|
|
}
|
|
// Push to value
|
|
this.renderResult();
|
|
}
|
|
// Function to buffer an operator given a value
|
|
this.bufferOperator = (value) =>
|
|
{
|
|
switch( value ) {
|
|
// If the value is Clear, reset all values to default.
|
|
case "C":
|
|
this.buffer = "0";
|
|
this.current = 0;
|
|
this.previousOperator = null;
|
|
break;
|
|
// If the value is equals, set previous operator to null,
|
|
// flush the operation using the typecasted buffer, and set
|
|
// buffer to the current value in order to continue operations.
|
|
// Finally set current value and previous operator to default.
|
|
case "=":
|
|
if( this.previousOperator === null ) {
|
|
return;
|
|
}
|
|
this.flushOperation( parseInt( this.buffer ));
|
|
this.buffer = "" + this.current;
|
|
this.previousOperator = null;
|
|
this.current = 0;
|
|
break;
|
|
// If value is backspace, delete the last digit in the buffer.
|
|
case "←":
|
|
if( this.buffer.length === 1 ) {
|
|
this.buffer = 0;
|
|
}
|
|
else {
|
|
this.buffer = this.buffer.substring( 0, this.buffer.length - 1 );
|
|
}
|
|
break;
|
|
default:
|
|
this.calculate( value );
|
|
}
|
|
}
|
|
// Adds operand, given a value, to the buffer.
|
|
this.bufferOperand = ( value ) =>
|
|
{
|
|
if( this.buffer === "0" )
|
|
{
|
|
this.buffer = value;
|
|
}
|
|
else
|
|
{
|
|
this.buffer += value;
|
|
}
|
|
}
|
|
// Performs operator functions depending on which operator was clicked.
|
|
// Given a tempBuffer, perform the corresponding operation to the current value.
|
|
this.flushOperation = ( tempBuffer ) => {
|
|
if( this.previousOperator === "+" ) { this.current += tempBuffer}
|
|
else if( this.previousOperator === "-" ) { this.current -= tempBuffer}
|
|
else if( this.previousOperator === "x" ) { this.current *= tempBuffer}
|
|
else { this.current /= tempBuffer}
|
|
}
|
|
// Given a value, create a temporary buffer equal to current buffer,
|
|
// perform the corresponding operation, and reset operator and buffer to
|
|
// default values.
|
|
this.calculate = ( value ) => {
|
|
const tempBuffer = parseInt( this.buffer );
|
|
if ( this.current === 0 ) {
|
|
this.current = tempBuffer;
|
|
}
|
|
else {
|
|
this.flushOperation(tempBuffer)
|
|
}
|
|
this.previousOperator = value;
|
|
this.buffer = "0";
|
|
}
|
|
// Exclusively called when a button is clicked, display a value
|
|
// rounded to 4 decimal places in the result box.
|
|
this.renderResult = () => {
|
|
// Note: this is the proper way to round to decimal places. Math.round( ( x * 100 ) / 100 )
|
|
// will almost always give rounding errors
|
|
result.value = +(Math.round(this.buffer + 'e+4') + 'e-4');
|
|
}
|
|
}
|
|
|
|
// Instantiate Calculator
|
|
let calculator = new Calculator();
|
|
|
|
// Create constant for result display HTML object
|
|
const result = document.querySelector("#result");
|
|
|
|
// Create Event Listener for button
|
|
document.querySelector( '.buttons' ).addEventListener( 'click', function( event ) {
|
|
// Pass the value of the HTML button to the buttonClicked() function.
|
|
calculator.buttonClicked( event.target.innerHTML );
|
|
}); |