From 1dbcdfefbcf8d47b934d4b588a47de9dd4c408bb Mon Sep 17 00:00:00 2001 From: bwcsemaj Date: Tue, 30 Jun 2020 22:18:07 -0400 Subject: [PATCH 1/3] Finished implementing, passed all tests, might have missed some edge cases --- assets/js/footer-functions.js | 7 +- assets/js/numbers.js | 187 +++++++++++++++++++++++++++++++++- 2 files changed, 192 insertions(+), 2 deletions(-) diff --git a/assets/js/footer-functions.js b/assets/js/footer-functions.js index 3587073..fbb1278 100644 --- a/assets/js/footer-functions.js +++ b/assets/js/footer-functions.js @@ -45,9 +45,14 @@ function additionAndSubtractionTest() { function additionAndMultiplicationTest() { - testCompute(0, "1+1*2"); + testCompute(3, "1+1*2"); testCompute(16, "10+6*1"); testCompute(34, "10+2*12"); + + //Added tests + testCompute(16, "10+6*1"); + testCompute(58, "10+2*12*2"); + } function subtractionAndMultiplicationTest() { diff --git a/assets/js/numbers.js b/assets/js/numbers.js index 5a925a6..026dfea 100644 --- a/assets/js/numbers.js +++ b/assets/js/numbers.js @@ -1,3 +1,188 @@ function compute(expression) { - // TODO - write method definition here + //Handle all multiplication and division first + //Find multiplication and division, find numbers left and right of + //the operator, perform operation, replace number, operator, number + //with value + console.log(expression); + expression = convertMinusToPlusMinus(expression); + console.log(expression); + expression = substituteMultiplicationAndDivision(expression.split("")); + console.log(expression); + var sum = sumAllIntegersTogether(expression.split("+")); + return sum; +} + +function sumAllIntegersTogether(expressionArray){ + var sum = 0; + for(var index = 0; expressionArray.length > index; index++){ + sum += parseFloat(expressionArray[index], 10) + } + return sum; +} + + +function convertMinusToPlusMinus(expression) { + var expressionArray = expression.split(""); + expression = expressionArray[0]; + var isPreviousNumber = Number.isInteger(parseInt(expressionArray[0])); + for (var index = 1; expressionArray.length > index; index++) { + if (isPreviousNumber && expressionArray[index] === "-") { + expression += "+-"; + isPreviousNumber = false; + continue; + } + expression += expressionArray[index]; + isPreviousNumber = Number.isInteger(parseInt(expressionArray[0])); + } + return expression; +} + + +// function amountOfOperators(expression) { +// var count = 0; +// var expressionArray = expression.split(""); +// for (var index = 0; expressionArray.length > index; index++) { +// var character = expressionArray[index]; +// if (character === "+" || character === "-" || character === "*" || character === "/") { +// count++; +// } +// } +// return count > 1; +// } + +// function hasMultiplication(expression) { +// var expressionArray = expression.split(""); +// for (var index = 0; expressionArray.length > index; index++) { +// var character = expressionArray[index]; +// if (character !== "*") { +// continue; +// } else { +// continue; +// } + +// } +// return false; +// } + +function findMultiDiv(expressionArray) { + for (var index = 0; expressionArray.length > index; index++) { + var character = expressionArray[index]; + var combineFunction = null; + if (character === "*" || character === "/") { + return index; + } + } + return undefined; +} + +function substituteMultiplicationAndDivision(expressionArray) { + + var index = findMultiDiv(expressionArray); + while (index !== undefined) { + var character = expressionArray[index]; + var combineFunction = character === "*" ? multi : div; + + + //Determine left number + var leftOperatorIndex = findLeftOperatorIndex(expressionArray, index); + console.log(leftOperatorIndex); + var leftOperatorCharacter = expressionArray[leftOperatorIndex]; + var leftNumber; + var leftStart; + if (leftOperatorCharacter === undefined) { + leftStart = 0; + leftNumber = convertToInt(expressionArray.slice(leftStart, index)); + } else { + leftStart = leftOperatorIndex + 1; + leftNumber = convertToInt(expressionArray.slice(leftStart, index)); + } + console.log("LEFT NUMBER: " + leftNumber); + + //Determine right number + //Check if negative + var rightOperatorIndex = findRightOperatorIndex(expressionArray, index); + console.log("RIGHT INDEX: " + rightOperatorIndex); + var rightOperatorCharacter = expressionArray[rightOperatorIndex]; + var rightNumber; + var rightEnd; + if (rightOperatorIndex === undefined) { + rightEnd = expressionArray.length - 1; + var rightSlice = expressionArray.slice(index + 1, rightEnd + 1); + rightNumber = convertToInt(rightSlice); + } else { + rightEnd = rightOperatorIndex - 1; + rightNumber = convertToInt(expressionArray.slice(index + 1, rightEnd + 1)); + } + console.log("RIGHT NUMBER: " + rightNumber); + + // //Combine + var number = combineFunction(leftNumber, rightNumber); + var numberArray = ("" + number).split(""); + console.log("COMBINE: " + number); + + //Create new Array + var newExpressionArray = new Array(expressionArray.length - (rightEnd - leftStart) + numberArray.length); + var added = 0; + console.log(expressionArray); + for (var index = 0; expressionArray.length > index; index++) { + if (index < leftStart || index > rightEnd) { + newExpressionArray[added] = expressionArray[index]; + added++; + } else if (index == leftStart) { + for (var numberIndex = 0; numberArray.length > numberIndex; numberIndex++) { + newExpressionArray[added] = numberArray[numberIndex]; + added++; + } + } + } + console.log(newExpressionArray); + expressionArray = newExpressionArray; + index = findMultiDiv(expressionArray); + } + return expressionArray.join(""); +} + +function multi(value, value2) { + return value * value2; +} + +function div(value, value2) { + return value / value2; +} + + +function findLeftOperatorIndex(expressionArray, end) { + //Find the position where the first variable starts + var posOperatorIndex = --end; + while (posOperatorIndex >= 0) { + var posOperator = expressionArray[posOperatorIndex]; + if (posOperator === "+") { + return posOperatorIndex; + } + posOperatorIndex--; + } + return undefined; +} + +function findRightOperatorIndex(expressionArray, start) { + //Find the position where the first variable starts + //Check to see if first right is negative symbol (if so skip over it) + if (expressionArray[start + 1] === "-") { + start++; + } + + //Initialize Start + var posOperatorIndex = ++start; + while (expressionArray.length > posOperatorIndex) { + var posOperator = expressionArray[posOperatorIndex]; + if (posOperator === "/" || posOperator === "+" || posOperator === "*") { + return posOperatorIndex; + } + posOperatorIndex++; + } + return undefined; +} + +function convertToInt(integerStringArray) { + return parseFloat(integerStringArray.join(""), 10); } \ No newline at end of file From ec5f6d7f0209ca7d4cab18553524f648dfd53290 Mon Sep 17 00:00:00 2001 From: bwcsemaj Date: Wed, 1 Jul 2020 09:09:42 -0400 Subject: [PATCH 2/3] making code cleaner part 1 --- assets/js/numbers.js | 49 +++++++++++--------------------------------- 1 file changed, 12 insertions(+), 37 deletions(-) diff --git a/assets/js/numbers.js b/assets/js/numbers.js index 026dfea..fe1fde1 100644 --- a/assets/js/numbers.js +++ b/assets/js/numbers.js @@ -3,16 +3,18 @@ function compute(expression) { //Find multiplication and division, find numbers left and right of //the operator, perform operation, replace number, operator, number //with value - console.log(expression); + console.log("START : " + expression); + expression = expression.replace("--", "+"); + console.log("REMOVE -- : " + expression); expression = convertMinusToPlusMinus(expression); - console.log(expression); + console.log("CONVERT - to +- :" + expression); expression = substituteMultiplicationAndDivision(expression.split("")); - console.log(expression); - var sum = sumAllIntegersTogether(expression.split("+")); + console.log("SUB MULTI/DIV : " + expression); + var sum = sumAll(expression.split("+")); return sum; } -function sumAllIntegersTogether(expressionArray){ +function sumAll(expressionArray){ var sum = 0; for(var index = 0; expressionArray.length > index; index++){ sum += parseFloat(expressionArray[index], 10) @@ -37,33 +39,6 @@ function convertMinusToPlusMinus(expression) { return expression; } - -// function amountOfOperators(expression) { -// var count = 0; -// var expressionArray = expression.split(""); -// for (var index = 0; expressionArray.length > index; index++) { -// var character = expressionArray[index]; -// if (character === "+" || character === "-" || character === "*" || character === "/") { -// count++; -// } -// } -// return count > 1; -// } - -// function hasMultiplication(expression) { -// var expressionArray = expression.split(""); -// for (var index = 0; expressionArray.length > index; index++) { -// var character = expressionArray[index]; -// if (character !== "*") { -// continue; -// } else { -// continue; -// } - -// } -// return false; -// } - function findMultiDiv(expressionArray) { for (var index = 0; expressionArray.length > index; index++) { var character = expressionArray[index]; @@ -91,10 +66,10 @@ function substituteMultiplicationAndDivision(expressionArray) { var leftStart; if (leftOperatorCharacter === undefined) { leftStart = 0; - leftNumber = convertToInt(expressionArray.slice(leftStart, index)); + leftNumber = convertToFloat(expressionArray.slice(leftStart, index)); } else { leftStart = leftOperatorIndex + 1; - leftNumber = convertToInt(expressionArray.slice(leftStart, index)); + leftNumber = convertToFloat(expressionArray.slice(leftStart, index)); } console.log("LEFT NUMBER: " + leftNumber); @@ -108,10 +83,10 @@ function substituteMultiplicationAndDivision(expressionArray) { if (rightOperatorIndex === undefined) { rightEnd = expressionArray.length - 1; var rightSlice = expressionArray.slice(index + 1, rightEnd + 1); - rightNumber = convertToInt(rightSlice); + rightNumber = convertToFloat(rightSlice); } else { rightEnd = rightOperatorIndex - 1; - rightNumber = convertToInt(expressionArray.slice(index + 1, rightEnd + 1)); + rightNumber = convertToFloat(expressionArray.slice(index + 1, rightEnd + 1)); } console.log("RIGHT NUMBER: " + rightNumber); @@ -183,6 +158,6 @@ function findRightOperatorIndex(expressionArray, start) { return undefined; } -function convertToInt(integerStringArray) { +function convertToFloat(integerStringArray) { return parseFloat(integerStringArray.join(""), 10); } \ No newline at end of file From 2f569b0669c0a25cbbd4015d423a293c9a077b7d Mon Sep 17 00:00:00 2001 From: bwcsemaj Date: Wed, 1 Jul 2020 10:44:52 -0400 Subject: [PATCH 3/3] test --- assets/js/numbers.js | 1 + 1 file changed, 1 insertion(+) diff --git a/assets/js/numbers.js b/assets/js/numbers.js index fe1fde1..563cf1a 100644 --- a/assets/js/numbers.js +++ b/assets/js/numbers.js @@ -11,6 +11,7 @@ function compute(expression) { expression = substituteMultiplicationAndDivision(expression.split("")); console.log("SUB MULTI/DIV : " + expression); var sum = sumAll(expression.split("+")); + console.log("SUM : " + sum); return sum; }