Skip to content

Commit

Permalink
refactor operand implementation to better support keyboard input and …
Browse files Browse the repository at this point in the history
…fix display issues
  • Loading branch information
METROIDHunter9000 committed Apr 19, 2024
1 parent e0131a8 commit b8461e2
Showing 1 changed file with 35 additions and 26 deletions.
61 changes: 35 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ class FSMState {
// Operand represents a number with a sign
class Operand {
constructor(val, sign) {
this.val = val;
this.valMajor = String(val);
this.sign = sign;
this.valMinor = '';
}

realVal() {
let realVal = +this.valMajor + +this.valMinor / 10**this.valMinor.length;

// "True" indiciates a positive sign. False is negative
if(this.sign){
return this.val;
return realVal;
}else{
return -1 * this.val;
return -1 * realVal;
}
}
}
Expand Down Expand Up @@ -116,7 +119,7 @@ stateOpr = new FSMState(input => {
selectOperator(input);

}else if(input === '.'){
operand2 = new Operand(0, false);
operand2 = new Operand(0, true);
freezeDecimal();
return stateOp2;

Expand Down Expand Up @@ -160,7 +163,7 @@ stateOp2 = new FSMState(input => {
percentify(operand2);

}else if(input === 'Backspace'){
if(operand2.val != 0){
if(operand2.realVal() != 0){
removeDigit(operand2);
}else{
operand2 = null;
Expand Down Expand Up @@ -215,7 +218,7 @@ stateR = new FSMState(input => {

// Initialize state
let fsmState = stateOp1;
let fractional_mode = 0;
let fractional_mode = false;
let operand1;
let operand2;
let operator;
Expand Down Expand Up @@ -247,41 +250,39 @@ function updateDisplay() {
}

function enterDigit(operand, digit) {
if(fractional_mode === 0){
operand.val *= 10;
operand.val += digit;
}else if(fractional_mode > 0){
operand.val += digit / 10**fractional_mode++;
if(!fractional_mode){
operand.valMajor += digit;
}else{
throw `Illegal value for fractional_mode: ${fractional_mode}`
operand.valMinor += digit;
}
updateDisplay();
}

function removeDigit(operand){
if(fractional_mode === 0){
operand.val = Math.floor(operand.val / 10);
if(!operand.valMinor){
fractional_mode = false;
operand.valMajor = operand.valMajor.slice(0, -1);
}else{
operand.val = +operand.val.toFixed(fractional_mode - 2);
fractional_mode--;
if(fractional_mode === 1){
fractional_mode = 0;
fractional_mode = true;
operand.valMinor = operand.valMinor.slice(0, -1);
if(operand.valMinor){
fractional_mode = false;
}
}
updateDisplay();
}

function selectOperator(op) {
operator = Operators[op];
fractional_mode = 0;
fractional_mode = false;
updateDisplay();
}

function clear() {
operand1 = new Operand(0, true);
operand2 = null;
operator = null;
fractional_mode = 0;
fractional_mode = false;
updateDisplay();
}

Expand All @@ -291,17 +292,25 @@ function negate(operand) {
}

function percentify(operand) {
operand.val /= 100;
if(fractional_mode == 0){
fractional_mode = 1;
let shift = operand.valMajor.slice(-2)
if(shift.length == 1){
shift = `0${shift}`;
}
operand.valMajor = operand.valMajor.slice(0, -2);
if(operand.valMajor === ''){
operand.valMajor = '0';
}
operand.valMinor = `${shift}${operand.valMinor}`

if(fractional_mode == false){
fractional_mode = true;
}
fractional_mode += 2;
updateDisplay();
}

function freezeDecimal() {
if(fractional_mode == 0){
fractional_mode = 1;
if(!fractional_mode){
fractional_mode = true;
}
updateDisplay();
}
Expand Down

0 comments on commit b8461e2

Please sign in to comment.