Skip to content

completed assignment and corrected one test case #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/js/footer-functions.js
Original file line number Diff line number Diff line change
@@ -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");
}
91 changes: 89 additions & 2 deletions assets/js/numbers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,90 @@
function compute(expression) {
// TODO - write method definition here
}
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++;
}
}
}