From 7af51ae5abf4dc4ef306570862485d026628c33e Mon Sep 17 00:00:00 2001 From: Laura Godinez Date: Tue, 30 Jun 2020 23:50:12 -0400 Subject: [PATCH 1/2] First edits on numbers work --- assets/js/footer-functions.js | 72 ++++++++++++--------------- assets/js/numbers.js | 91 ++++++++++++++++++++++++++++++++++- 2 files changed, 120 insertions(+), 43 deletions(-) diff --git a/assets/js/footer-functions.js b/assets/js/footer-functions.js index 3587073..21a0d92 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(0, "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..98e88a8 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 + var total = 0; + + //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 = + parseInt(charactersToProcess[firstOperandIndex]) * + parseInt(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 = + parseInt(charactersToProcess[firstOperandIndex]) / + parseInt(charactersToProcess[secondOperandIndex]); + + charactersToProcess.splice(firstOperandIndex, 3, result); + // debugger; + // console.log(result); + } + } + + while ( + charactersToProcess.indexOf("+") !== -1 || + charactersToProcess.indexOf("-") !== -1 + ) { + opearandIndex = charactersToProcess.indexOf("+"); + if (charactersToProcess[opearandIndex] == "+") { + firstOperandIndex = opearandIndex - 1; + secondOperandIndex = opearandIndex + 1; + result = + parseInt(charactersToProcess[firstOperandIndex]) + + parseInt(charactersToProcess[secondOperandIndex]); + + charactersToProcess.splice(firstOperandIndex, 3, result); + // debugger; + // console.log(result); + } + opearandIndex = charactersToProcess.indexOf("-"); + if (charactersToProcess[opearandIndex] == "-") { + firstOperandIndex = opearandIndex - 1; + secondOperandIndex = opearandIndex + 1; + result = + parseInt(charactersToProcess[firstOperandIndex]) - + parseInt(charactersToProcess[secondOperandIndex]); + + charactersToProcess.splice(firstOperandIndex, 3, result); + // debugger; + // console.log(result); + } + total = charactersToProcess[0]; + return total; + } + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice +} From 2f077bc89546f934a4e8d84f22c13151f3829da2 Mon Sep 17 00:00:00 2001 From: Laura Godinez Date: Wed, 1 Jul 2020 00:27:47 -0400 Subject: [PATCH 2/2] Finished edits --- assets/js/footer-functions.js | 2 +- assets/js/numbers.js | 81 ++++++++++++++++++++++------------- 2 files changed, 53 insertions(+), 30 deletions(-) diff --git a/assets/js/footer-functions.js b/assets/js/footer-functions.js index 21a0d92..90295cf 100644 --- a/assets/js/footer-functions.js +++ b/assets/js/footer-functions.js @@ -40,7 +40,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 98e88a8..6a77b78 100644 --- a/assets/js/numbers.js +++ b/assets/js/numbers.js @@ -1,6 +1,4 @@ function compute(expression) { - var total = 0; - //1. Split each char into an array // Input "3-2-3*10/2+10" // var charactersToProcess = [ @@ -33,8 +31,8 @@ function compute(expression) { var firstOperandIndex = opearandIndex - 1; var secondOperandIndex = opearandIndex + 1; var result = - parseInt(charactersToProcess[firstOperandIndex]) * - parseInt(charactersToProcess[secondOperandIndex]); + parseFloat(charactersToProcess[firstOperandIndex]) * + parseFloat(charactersToProcess[secondOperandIndex]); charactersToProcess.splice(firstOperandIndex, 3, result); // debugger; @@ -46,8 +44,8 @@ function compute(expression) { firstOperandIndex = opearandIndex - 1; secondOperandIndex = opearandIndex + 1; result = - parseInt(charactersToProcess[firstOperandIndex]) / - parseInt(charactersToProcess[secondOperandIndex]); + parseFloat(charactersToProcess[firstOperandIndex]) / + parseFloat(charactersToProcess[secondOperandIndex]); charactersToProcess.splice(firstOperandIndex, 3, result); // debugger; @@ -59,32 +57,57 @@ function compute(expression) { charactersToProcess.indexOf("+") !== -1 || charactersToProcess.indexOf("-") !== -1 ) { - opearandIndex = charactersToProcess.indexOf("+"); - if (charactersToProcess[opearandIndex] == "+") { - firstOperandIndex = opearandIndex - 1; - secondOperandIndex = opearandIndex + 1; - result = - parseInt(charactersToProcess[firstOperandIndex]) + - parseInt(charactersToProcess[secondOperandIndex]); + addOpearandIndex = charactersToProcess.indexOf("+"); + subOpearandIndex = charactersToProcess.indexOf("-"); - charactersToProcess.splice(firstOperandIndex, 3, result); - // debugger; - // console.log(result); - } - opearandIndex = charactersToProcess.indexOf("-"); - if (charactersToProcess[opearandIndex] == "-") { - firstOperandIndex = opearandIndex - 1; - secondOperandIndex = opearandIndex + 1; - result = - parseInt(charactersToProcess[firstOperandIndex]) - - parseInt(charactersToProcess[secondOperandIndex]); + 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); - // debugger; - // console.log(result); + 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); + } } - total = charactersToProcess[0]; - return total; } + return charactersToProcess[0]; // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice }