From fa40223ec40b58f0badd5f78a61a5103ca52dcdb Mon Sep 17 00:00:00 2001 From: Ghassan Nasr Date: Tue, 30 Jun 2020 23:42:44 -0400 Subject: [PATCH] completed assignment and corrected one test case --- assets/js/footer-functions.js | 2 +- assets/js/numbers.js | 91 ++++++++++++++++++++++++++++++++++- 2 files changed, 90 insertions(+), 3 deletions(-) diff --git a/assets/js/footer-functions.js b/assets/js/footer-functions.js index 3587073..3b78ea2 100644 --- a/assets/js/footer-functions.js +++ b/assets/js/footer-functions.js @@ -45,7 +45,7 @@ function additionAndSubtractionTest() { function additionAndMultiplicationTest() { - testCompute(0, "1+1*2"); + testCompute(3, "1+1*2"); testCompute(16, "10+6*1"); testCompute(34, "10+2*12"); } diff --git a/assets/js/numbers.js b/assets/js/numbers.js index 5a925a6..b02fd1d 100644 --- a/assets/js/numbers.js +++ b/assets/js/numbers.js @@ -1,3 +1,90 @@ function compute(expression) { - // TODO - write method definition here -} \ No newline at end of file + let exp = new Expression(expression); + + exp.doHighPrecedenceOperations(); + exp.doLowPrecedenceOperations(); + + return parseFloat(exp.expArr[0]); +} + + +class Expression { + constructor(expString) { + this.expArr = this.parseStringExpressionToArray(expString); + } + + parseStringExpressionToArray(expString) { + + //will create two arrays, one of all the operands + //and another of all the operators, then merge them + + //create a copy of string expression first + let temp = expString; + + //replace all clusters of digits with underscores + expString = expString.replace(/[0-9]+/g, "_"); + + //create an array of the operators only by splitting on the underscore + //and filtering out the empty values in the resulting array + let operators = expString.split("_").filter(function(str){return str != ""}); + + //create an array of all the strings of numbers that were parsed out + let operands = temp.split(/[^0-9\.]+/); + + //now ready to merge the operands and operators in a new array "result" + let result = []; + + for(let i = 0; i < operands.length; i++){ + //for every operand, push and operator, except when the + //last operand is reached + result.push(operands[i]); + if (i < operators.length) result.push(operators[i]); + } + + return result; + } + + doHighPrecedenceOperations() { + let currIndex = 0; + while(currIndex < this.expArr.length-1) { + let currValue = this.expArr[currIndex]; + if(currValue == "*" || currValue == "/") { + let result; + let operand1 = parseFloat(this.expArr[currIndex-1], 10); + let operand2 = parseFloat(this.expArr[currIndex+1], 10); + if(currValue == "*") + result = operand1 * operand2; + else + result = operand1 / operand2; + + //insert the result at the first operand's position and remove three elements + result = result.toString(); + this.expArr.splice(currIndex-1, 3, result); + } + else + currIndex++; + } + } + + doLowPrecedenceOperations() { + let currIndex = 0; + while(currIndex < this.expArr.length-1) { + let currValue = this.expArr[currIndex]; + if(currValue == "+" || currValue == "-") { + let result; + let operand1 = parseFloat(this.expArr[currIndex-1], 10); + let operand2 = parseFloat(this.expArr[currIndex+1], 10); + if(currValue == "-") + result = operand1 - operand2; + else + result = operand1 + operand2; + + //insert the result at the first operand's position and remove three elements + result = result.toString(); + this.expArr.splice(currIndex-1, 3, result); + } + else + currIndex++; + } + } +}