diff --git a/assets/js/footer-functions.js b/assets/js/footer-functions.js index 3587073..90295cf 100644 --- a/assets/js/footer-functions.js +++ b/assets/js/footer-functions.js @@ -9,72 +9,62 @@ subtractionAndMultiplicationTest(); subtractionAndDivisonTest(); allOperatorTest(); - - - function additionTest() { - testCompute(2, "1+1"); - testCompute(3, "1+1+1"); - testCompute(6, "3+2+1"); + testCompute(2, "1+1"); + testCompute(3, "1+1+1"); + testCompute(6, "3+2+1"); } function subtractionTest() { - testCompute(0, "1-1"); - testCompute(-1, "1-1-1"); - testCompute(2, "5-2-1"); + testCompute(0, "1-1"); + testCompute(-1, "1-1-1"); + testCompute(2, "5-2-1"); } function multiplicationTest() { - testCompute(1, "1*1"); - testCompute(2, "1*1*2"); - testCompute(50, "5*5*2"); + testCompute(1, "1*1"); + testCompute(2, "1*1*2"); + testCompute(50, "5*5*2"); } function divisionTest() { - testCompute(1, "1/1"); - testCompute(1, "10/2/5"); - testCompute(10, "20/2"); + testCompute(1, "1/1"); + testCompute(1, "10/2/5"); + testCompute(10, "20/2"); } - function additionAndSubtractionTest() { - testCompute(0, "1+1-2"); - testCompute(15, "10+6-1"); - testCompute(20, "10-2+12"); + testCompute(0, "1+1-2"); + testCompute(15, "10+6-1"); + testCompute(20, "10-2+12"); } - function additionAndMultiplicationTest() { - testCompute(0, "1+1*2"); - testCompute(16, "10+6*1"); - testCompute(34, "10+2*12"); + testCompute(3, "1+1*2"); + testCompute(16, "10+6*1"); + testCompute(34, "10+2*12"); } function subtractionAndMultiplicationTest() { - testCompute(-1, "1-1*2"); - testCompute(4, "10-6*1"); - testCompute(-14, "10-2*12"); + testCompute(-1, "1-1*2"); + testCompute(4, "10-6*1"); + testCompute(-14, "10-2*12"); } - - - - function additionAndDivisonTest() { - testCompute(1.5, "1+1/2"); - testCompute(16, "10+6/1"); - testCompute(10.5, "10+2/4"); + testCompute(1.5, "1+1/2"); + testCompute(16, "10+6/1"); + testCompute(10.5, "10+2/4"); } function subtractionAndDivisonTest() { - testCompute(0.5, "1-1/2"); - testCompute(4, "10-6/1"); - testCompute(9.5, "10-2/4"); + testCompute(0.5, "1-1/2"); + testCompute(4, "10-6/1"); + testCompute(9.5, "10-2/4"); } - function allOperatorTest() { - testCompute(-12, "1+2-3*10/2"); - testCompute(-11, "1+3-3*10/2"); - testCompute(-4, "3-2-3*10/2+10"); -} \ No newline at end of file + testCompute(-12, "1+2-3*10/2"); + testCompute(-11, "1+3-3*10/2"); + testCompute(-4, "3-2-3*10/2+10"); +} diff --git a/assets/js/numbers.js b/assets/js/numbers.js index 5a925a6..6a77b78 100644 --- a/assets/js/numbers.js +++ b/assets/js/numbers.js @@ -1,3 +1,113 @@ function compute(expression) { - // TODO - write method definition here -} \ No newline at end of file + //1. Split each char into an array + // Input "3-2-3*10/2+10" + // var charactersToProcess = [ + // "3", + // "-", + // "2", + // "-", + // "3", + // "*", + // "10", + // "/", + // "2", + // "+", + // "10" + // ]; + + var charactersToProcess = expression + // RegEx helper + .replace(/\'/g, "") + .split(/(\d+)/) + .filter(Boolean); + + //2. Process Multiplication and Division Operations remove them from remaining chars and aggregate total + while ( + charactersToProcess.indexOf("*") !== -1 || + charactersToProcess.indexOf("/") !== -1 + ) { + var opearandIndex = charactersToProcess.indexOf("*"); + if (charactersToProcess[opearandIndex] == "*") { + var firstOperandIndex = opearandIndex - 1; + var secondOperandIndex = opearandIndex + 1; + var result = + parseFloat(charactersToProcess[firstOperandIndex]) * + parseFloat(charactersToProcess[secondOperandIndex]); + + charactersToProcess.splice(firstOperandIndex, 3, result); + // debugger; + // console.log(result); + } + opearandIndex = ""; + opearandIndex = charactersToProcess.indexOf("/"); + if (charactersToProcess[opearandIndex] == "/") { + firstOperandIndex = opearandIndex - 1; + secondOperandIndex = opearandIndex + 1; + result = + parseFloat(charactersToProcess[firstOperandIndex]) / + parseFloat(charactersToProcess[secondOperandIndex]); + + charactersToProcess.splice(firstOperandIndex, 3, result); + // debugger; + // console.log(result); + } + } + + while ( + charactersToProcess.indexOf("+") !== -1 || + charactersToProcess.indexOf("-") !== -1 + ) { + addOpearandIndex = charactersToProcess.indexOf("+"); + subOpearandIndex = charactersToProcess.indexOf("-"); + + if ( + addOpearandIndex != -1 && + subOpearandIndex != -1 && + addOpearandIndex < subOpearandIndex + ) { + if (charactersToProcess[addOpearandIndex] == "+") { + firstOperandIndex = addOpearandIndex - 1; + secondOperandIndex = addOpearandIndex + 1; + result = + parseFloat(charactersToProcess[firstOperandIndex]) + + parseFloat(charactersToProcess[secondOperandIndex]); + + charactersToProcess.splice(firstOperandIndex, 3, result); + } + } else if ( + addOpearandIndex != -1 && + subOpearandIndex != -1 && + subOpearandIndex < addOpearandIndex + ) { + if (charactersToProcess[subOpearandIndex] == "-") { + firstOperandIndex = subOpearandIndex - 1; + secondOperandIndex = subOpearandIndex + 1; + result = + parseFloat(charactersToProcess[firstOperandIndex]) - + parseFloat(charactersToProcess[secondOperandIndex]); + + charactersToProcess.splice(firstOperandIndex, 3, result); + } + } else { + if (addOpearandIndex !== -1) { + firstOperandIndex = addOpearandIndex - 1; + secondOperandIndex = addOpearandIndex + 1; + result = + parseFloat(charactersToProcess[firstOperandIndex]) + + parseFloat(charactersToProcess[secondOperandIndex]); + + charactersToProcess.splice(firstOperandIndex, 3, result); + } else if (subOpearandIndex !== -1) { + firstOperandIndex = subOpearandIndex - 1; + secondOperandIndex = subOpearandIndex + 1; + result = + parseFloat(charactersToProcess[firstOperandIndex]) - + parseFloat(charactersToProcess[secondOperandIndex]); + + charactersToProcess.splice(firstOperandIndex, 3, result); + } + } + } + return charactersToProcess[0]; + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice +}