add guided calculator class
This commit is contained in:
parent
cfb13f2812
commit
4f15f98a7b
1 changed files with 101 additions and 0 deletions
101
calculator.js
Normal file
101
calculator.js
Normal file
|
@ -0,0 +1,101 @@
|
||||||
|
class Calculator {
|
||||||
|
result = 0;
|
||||||
|
expression = [];
|
||||||
|
|
||||||
|
#config;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Perform a calculation.
|
||||||
|
*/
|
||||||
|
calculate(parameters) {
|
||||||
|
this.input = this.expression.join(``);
|
||||||
|
|
||||||
|
// Evaluate.
|
||||||
|
try {
|
||||||
|
this.result = eval(this.input);
|
||||||
|
delete this.input;
|
||||||
|
|
||||||
|
if (this.result === Infinity || this.result === NaN) {
|
||||||
|
throw new Error(undefined);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (this.result);
|
||||||
|
} catch(err) {
|
||||||
|
alert(err);
|
||||||
|
return(err);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
Execute special functions.
|
||||||
|
|
||||||
|
@param {string} operation The function name to execute.
|
||||||
|
@param {string} passthrough The value to pass through.
|
||||||
|
*/
|
||||||
|
run (operation, passthrough) {
|
||||||
|
if ((operation) ? operation.trim() : false) {
|
||||||
|
switch (operation) {
|
||||||
|
case `AC`:
|
||||||
|
this.expression = [];
|
||||||
|
break;
|
||||||
|
case `=`:
|
||||||
|
this.result = this.calculate((passthrough) ? passthrough : this.expression);
|
||||||
|
this.expression = [this.result];
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Allow the users to input data.
|
||||||
|
*/
|
||||||
|
interactive() {
|
||||||
|
while (true) {
|
||||||
|
this.input = prompt((this.expression.length) ? this.expression.join(` `) : `0`);
|
||||||
|
|
||||||
|
// Input number first. Usually, this can be determined if the number length is the same as the operand length.
|
||||||
|
if ((this.input) ? this.input.trim() : false) {
|
||||||
|
this.input = this.input.trim();
|
||||||
|
|
||||||
|
if ((this.#config[`operands`].includes(this.input)) || (!(this.#config[`functions`].includes(this.input)))) {
|
||||||
|
if (!((this.#config[`operands`].includes(this.input)) || (this.#config[`order`].includes(this.input)))) {
|
||||||
|
try {
|
||||||
|
this.input = parseFloat(this.input);
|
||||||
|
} catch(err) {};
|
||||||
|
};
|
||||||
|
|
||||||
|
// Check if the last item is an operand.
|
||||||
|
if ((this.expression.length && (!(this.#config[`order`].includes(this.input)))) ? (((typeof this.input).includes(`number`)) ? ((typeof this.expression[this.expression.length - 1]).includes(`number`)) : this.#config[`operands`].includes(this.expression[this.expression.length - 1])) : false) {
|
||||||
|
this.expression[this.expression.length - 1] = this.input;
|
||||||
|
} else {
|
||||||
|
this.expression.push(this.input);
|
||||||
|
};
|
||||||
|
|
||||||
|
} else if (this.#config[`functions`].includes(this.input)) {
|
||||||
|
this.run(this.input);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
delete this.input;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.#config = {};
|
||||||
|
this.#config[`operands`] = [`+`, `-`, `*`, `/`, `%`, `^`];
|
||||||
|
this.#config[`order`] = [`(`, `)`];
|
||||||
|
this.#config[`functions`] = [`AC`, `=`];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
function main() {
|
||||||
|
let INSTANCE = new Calculator();
|
||||||
|
INSTANCE.interactive();
|
||||||
|
};
|
||||||
|
|
||||||
|
main();
|
Loading…
Add table
Add a link
Reference in a new issue