From d61c44fafe60d01e3d497b5e6301f2833015c064 Mon Sep 17 00:00:00 2001 From: Nkyo Lio Date: Wed, 1 Jul 2020 14:40:13 -0400 Subject: [PATCH 1/5] last update --- assets/js/numbers.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/assets/js/numbers.js b/assets/js/numbers.js index 5a925a6..e4f9f07 100644 --- a/assets/js/numbers.js +++ b/assets/js/numbers.js @@ -1,3 +1,32 @@ function compute(expression) { - // TODO - write method definition here + +let operators = new Array(); +for(let i = 0; i < expression.split(/[^\D]/).length; i++) { + if(expression.split(/[^\D]/)[i] != "") operators.push(expression.split(/[^\D]/)[i]); +} + +let numbers = expression.split(/\D/); +for(let i = 0; i < numbers.length; i++) { + numbers[i] = parseInt(numbers[i]); +} + +let result = numbers[0]; + +for(let i = 0; i < operators.length; i++) { + result = calculation(operators[i], result, numbers[i + 1]); +} +return result; +} + +function calculation(operator, operant1, operant2) { + switch(operator) { + case "+": + return operant1 + operant2; + case "-": + return operant1 - operant2; + case "*": + return operant1 * operant2; + case "/": + return operant1 / operant2; + } } \ No newline at end of file From 94a569a8f9b8915083b57cbb31e2bc750578d1c7 Mon Sep 17 00:00:00 2001 From: Nkyo Lio Date: Thu, 2 Jul 2020 15:21:48 -0400 Subject: [PATCH 2/5] Work in progress --- assets/js/numbers.js | 53 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/assets/js/numbers.js b/assets/js/numbers.js index e4f9f07..59f1a00 100644 --- a/assets/js/numbers.js +++ b/assets/js/numbers.js @@ -10,14 +10,45 @@ for(let i = 0; i < numbers.length; i++) { numbers[i] = parseInt(numbers[i]); } -let result = numbers[0]; +let jsonOp = jsonOfOperators(operators); +let result = 0; -for(let i = 0; i < operators.length; i++) { - result = calculation(operators[i], result, numbers[i + 1]); +if(jsonOp.multiplication == operators.length) { + result = numbers[0]; + for(let i = 0; i < operators.length; i++) { + result = calculation("*", result, numbers[i + 1]); + } + return result; +} + +if(jsonOp.division == operators.length) { + result = numbers[0]; + for(let i = 0; i < operators.length; i++) { + result = calculation("/", result, numbers[i + 1]); + } + return result; } -return result; + +if(jsonOp.addition == operators.length) { + result = numbers[0]; + for(let i = 0; i < operators.length; i++) { + result = calculation("+", result, numbers[i + 1]); + } + return result; } +if(jsonOp.subtraction == operators.length) { + result = numbers[0]; + for(let i = 0; i < operators.length; i++) { + result = calculation("-", result, numbers[i + 1]); + } + return result; +} + +return result; +} + +// Operate the calculation base on the operator function calculation(operator, operant1, operant2) { switch(operator) { case "+": @@ -28,5 +59,15 @@ function calculation(operator, operant1, operant2) { return operant1 * operant2; case "/": return operant1 / operant2; - } -} \ No newline at end of file + } +} + +// Count each operator occurence, will be use to determine the order of operation +let jsonOfOperators = (arrayOfOperators) => { + return { + multiplication: arrayOfOperators.filter(element => element == "*").length, + division: arrayOfOperators.filter(element => element == "/").length, + addition: arrayOfOperators.filter(element => element == "+").length, + subtraction: arrayOfOperators.filter(element => element == "-").length + }; +} From fcc2f90f5153dd01b2af17a915fe92668013d2b7 Mon Sep 17 00:00:00 2001 From: Nkyo Lio Date: Thu, 2 Jul 2020 17:52:58 -0400 Subject: [PATCH 3/5] Best testing result so far, Work still in progress --- assets/js/numbers.js | 73 +++++++++++++++++++++++++++++++------------- 1 file changed, 51 insertions(+), 22 deletions(-) diff --git a/assets/js/numbers.js b/assets/js/numbers.js index 59f1a00..937e7e0 100644 --- a/assets/js/numbers.js +++ b/assets/js/numbers.js @@ -11,38 +11,67 @@ for(let i = 0; i < numbers.length; i++) { } let jsonOp = jsonOfOperators(operators); -let result = 0; +let result = null; -if(jsonOp.multiplication == operators.length) { - result = numbers[0]; - for(let i = 0; i < operators.length; i++) { - result = calculation("*", result, numbers[i + 1]); +if(jsonOp.multiplication != 0) { + result = numbers[operators.indexOf("*")]; + for(let i = 0; i < jsonOp.multiplication; i++) { + result = calculation("*", result, numbers[operators.indexOf("*") + 1]); + numbers[operators.indexOf("*")] = result; + operators[operators.indexOf("*")] = ""; + console.log("++++++++++++++++"); + console.log(i + " * iteration operators = " + operators); + console.log(i + " * iteration numbers = " + numbers); + console.log("++++++++++++++++"); } - return result; -} + +} -if(jsonOp.division == operators.length) { - result = numbers[0]; - for(let i = 0; i < operators.length; i++) { - result = calculation("/", result, numbers[i + 1]); +if(jsonOp.division != 0) { + if(result == null) { + result = numbers[operators.indexOf("/")]; + } + for(let i = 0; i < jsonOp.division; i++) { + result = calculation("/", result, numbers[operators.indexOf("/") + 1]); + numbers[operators.indexOf("/")] = result; + operators[operators.indexOf("/")] = ""; + console.log("++++++++++++++++"); + console.log(i + " / iteration operators = " + operators); + console.log(i + " / iteration numbers = " + numbers); + console.log("++++++++++++++++"); } - return result; + } -if(jsonOp.addition == operators.length) { - result = numbers[0]; - for(let i = 0; i < operators.length; i++) { - result = calculation("+", result, numbers[i + 1]); +if(jsonOp.subtraction != 0) { + if(result == null) { + result = numbers[operators.indexOf("-")]; + } + for(let i = 0; i < jsonOp.subtraction; i++) { + result = calculation("-", result, numbers[operators.indexOf("-") + 1]); + numbers[operators.indexOf("-")] = result; + operators[operators.indexOf("-")] = ""; + console.log("++++++++++++++++"); + console.log(i + " - iteration operators = " + operators); + console.log(i + " - iteration numbers = " + numbers); + console.log("++++++++++++++++"); } - return result; } -if(jsonOp.subtraction == operators.length) { - result = numbers[0]; - for(let i = 0; i < operators.length; i++) { - result = calculation("-", result, numbers[i + 1]); +if(jsonOp.addition != 0) { + if(result == null) { + result = numbers[operators.indexOf("+")]; + } + for(let i = 0; i < jsonOp.addition; i++) { + console.log("result inside + =" + result); + result = calculation("+", result, numbers[operators.indexOf("+")]); + numbers[operators.indexOf("+")] = result; + operators[operators.indexOf("+")] = ""; + console.log("++++++++++++++++"); + console.log(i + " + iteration operators = " + operators); + console.log(i + " + iteration numbers = " + numbers); + console.log("++++++++++++++++"); } - return result; } return result; From fbeca29162c9d6e3673ea33ee57d64814059f938 Mon Sep 17 00:00:00 2001 From: Nkyo Lio Date: Fri, 3 Jul 2020 17:37:19 -0400 Subject: [PATCH 4/5] Done Done Done ... --- assets/js/numbers.js | 140 +++++++++++++++++++------------------------ 1 file changed, 63 insertions(+), 77 deletions(-) diff --git a/assets/js/numbers.js b/assets/js/numbers.js index 937e7e0..8ee8840 100644 --- a/assets/js/numbers.js +++ b/assets/js/numbers.js @@ -1,85 +1,29 @@ function compute(expression) { -let operators = new Array(); -for(let i = 0; i < expression.split(/[^\D]/).length; i++) { - if(expression.split(/[^\D]/)[i] != "") operators.push(expression.split(/[^\D]/)[i]); -} - -let numbers = expression.split(/\D/); -for(let i = 0; i < numbers.length; i++) { - numbers[i] = parseInt(numbers[i]); -} - -let jsonOp = jsonOfOperators(operators); -let result = null; - -if(jsonOp.multiplication != 0) { - result = numbers[operators.indexOf("*")]; - for(let i = 0; i < jsonOp.multiplication; i++) { - result = calculation("*", result, numbers[operators.indexOf("*") + 1]); - numbers[operators.indexOf("*")] = result; - operators[operators.indexOf("*")] = ""; - console.log("++++++++++++++++"); - console.log(i + " * iteration operators = " + operators); - console.log(i + " * iteration numbers = " + numbers); - console.log("++++++++++++++++"); + let operators = new Array(); + for (let i = 0; i < expression.split(/[^\D]/).length; i++) { + if (expression.split(/[^\D]/)[i] != "") operators.push(expression.split(/[^\D]/)[i]); } - -} -if(jsonOp.division != 0) { - if(result == null) { - result = numbers[operators.indexOf("/")]; - } - for(let i = 0; i < jsonOp.division; i++) { - result = calculation("/", result, numbers[operators.indexOf("/") + 1]); - numbers[operators.indexOf("/")] = result; - operators[operators.indexOf("/")] = ""; - console.log("++++++++++++++++"); - console.log(i + " / iteration operators = " + operators); - console.log(i + " / iteration numbers = " + numbers); - console.log("++++++++++++++++"); + let numbers = expression.split(/\D/); + for (let i = 0; i < numbers.length; i++) { + numbers[i] = parseInt(numbers[i]); } -} + let jsonOp = jsonOfOperators(operators); + let arrayOfUsedOperants = new Array(); + let result = null; -if(jsonOp.subtraction != 0) { - if(result == null) { - result = numbers[operators.indexOf("-")]; - } - for(let i = 0; i < jsonOp.subtraction; i++) { - result = calculation("-", result, numbers[operators.indexOf("-") + 1]); - numbers[operators.indexOf("-")] = result; - operators[operators.indexOf("-")] = ""; - console.log("++++++++++++++++"); - console.log(i + " - iteration operators = " + operators); - console.log(i + " - iteration numbers = " + numbers); - console.log("++++++++++++++++"); - } -} - -if(jsonOp.addition != 0) { - if(result == null) { - result = numbers[operators.indexOf("+")]; - } - for(let i = 0; i < jsonOp.addition; i++) { - console.log("result inside + =" + result); - result = calculation("+", result, numbers[operators.indexOf("+")]); - numbers[operators.indexOf("+")] = result; - operators[operators.indexOf("+")] = ""; - console.log("++++++++++++++++"); - console.log(i + " + iteration operators = " + operators); - console.log(i + " + iteration numbers = " + numbers); - console.log("++++++++++++++++"); - } -} + result = calcOnOperator( "*", result, numbers, operators, arrayOfUsedOperants, jsonOp.multiplication); + result = calcOnOperator("/", result, numbers, operators, arrayOfUsedOperants, jsonOp.division); + result = calcOnOperator("-", result, numbers, operators, arrayOfUsedOperants, jsonOp.subtraction); -return result; + return calcOnOperator("+",result, numbers, operators, arrayOfUsedOperants, jsonOp.addition); } // Operate the calculation base on the operator function calculation(operator, operant1, operant2) { - switch(operator) { + switch (operator) { case "+": return operant1 + operant2; case "-": @@ -88,15 +32,57 @@ function calculation(operator, operant1, operant2) { return operant1 * operant2; case "/": return operant1 / operant2; - } + } } -// Count each operator occurence, will be use to determine the order of operation +// Return a JSON object of each operator and its occurence, will be use to determine the order of operation let jsonOfOperators = (arrayOfOperators) => { return { - multiplication: arrayOfOperators.filter(element => element == "*").length, - division: arrayOfOperators.filter(element => element == "/").length, - addition: arrayOfOperators.filter(element => element == "+").length, - subtraction: arrayOfOperators.filter(element => element == "-").length - }; + multiplication: arrayOfOperators.filter(element => element == "*").length, + division: arrayOfOperators.filter(element => element == "/").length, + addition: arrayOfOperators.filter(element => element == "+").length, + subtraction: arrayOfOperators.filter(element => element == "-").length + }; +} + +// Check the current operator position from the calculation already done, and update the result array +let checkUpdateResult = (result, numbers, operators, arrayOfUsedOperants, operator) => { + + // Check the current operation base on the last result + if (result == numbers[operators.indexOf(operator)] || result == numbers[operators.indexOf(operator) + 1]) { + // When the current operation does affect the previous operation, update all the element already used in a calculation to the current result + // Also update the array of the index of operants already used + result = calculation(operator, numbers[operators.indexOf(operator)], numbers[operators.indexOf(operator) + 1]); + arrayOfUsedOperants.push(operators.indexOf(operator)); + arrayOfUsedOperants.push(operators.indexOf(operator) + 1); + for (let j = 0; j < arrayOfUsedOperants.length; j++) { + numbers[arrayOfUsedOperants[j]] = result; + } + } else { + // When the current operation does not affect the previous operation result, do not update all the element already used in a calculation to the current result + result = calculation(operator, numbers[operators.indexOf(operator)], numbers[operators.indexOf(operator) + 1]); + numbers[operators.indexOf(operator)] = result; + numbers[operators.indexOf(operator) + 1] = result; + } + // Delete the current operator to evoid using it twice + operators[operators.indexOf(operator)] = ""; } + +let calcOnOperator = (operator, result, numbers, operators, arrayOfUsedOperants, numberOfOccurence) => { + if (numberOfOccurence != 0) { + if (result == null) result = numbers[operators.indexOf(operator)]; + for (let i = 0; i < numberOfOccurence; i++) { + checkUpdateResult(result, numbers, operators, arrayOfUsedOperants, operator); + + // Debugger + // console.log("++++++++++++++++"); + // console.log(i + " " + operator + " iteration array of used indexes " + arrayOfUsedOperants); + // console.log(i + " " + operator + " iteration operators = " + operators); + // console.log(i + " " + operator + " iteration numbers = " + numbers); + // console.log("++++++++++++++++"); + } + } + return numbers[arrayOfUsedOperants[arrayOfUsedOperants.length - 1]]; +} + + From 46a39a1ae9e14969df1f49398c231ca774f00354 Mon Sep 17 00:00:00 2001 From: Nkyo Lio Date: Sat, 4 Jul 2020 15:03:50 -0400 Subject: [PATCH 5/5] Done --- assets/js/numbers.js | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/assets/js/numbers.js b/assets/js/numbers.js index 8ee8840..5697e6c 100644 --- a/assets/js/numbers.js +++ b/assets/js/numbers.js @@ -11,27 +11,27 @@ function compute(expression) { } let jsonOp = jsonOfOperators(operators); - let arrayOfUsedOperants = new Array(); + let arrayOfUsedoperands = new Array(); let result = null; - result = calcOnOperator( "*", result, numbers, operators, arrayOfUsedOperants, jsonOp.multiplication); - result = calcOnOperator("/", result, numbers, operators, arrayOfUsedOperants, jsonOp.division); - result = calcOnOperator("-", result, numbers, operators, arrayOfUsedOperants, jsonOp.subtraction); + result = calcOnOperator( "*", result, numbers, operators, arrayOfUsedoperands, jsonOp.multiplication); + result = calcOnOperator("/", result, numbers, operators, arrayOfUsedoperands, jsonOp.division); + result = calcOnOperator("-", result, numbers, operators, arrayOfUsedoperands, jsonOp.subtraction); - return calcOnOperator("+",result, numbers, operators, arrayOfUsedOperants, jsonOp.addition); + return calcOnOperator("+",result, numbers, operators, arrayOfUsedoperands, jsonOp.addition); } // Operate the calculation base on the operator -function calculation(operator, operant1, operant2) { +function calculation(operator, operand1, operand2) { switch (operator) { case "+": - return operant1 + operant2; + return operand1 + operand2; case "-": - return operant1 - operant2; + return operand1 - operand2; case "*": - return operant1 * operant2; + return operand1 * operand2; case "/": - return operant1 / operant2; + return operand1 / operand2; } } @@ -46,17 +46,17 @@ let jsonOfOperators = (arrayOfOperators) => { } // Check the current operator position from the calculation already done, and update the result array -let checkUpdateResult = (result, numbers, operators, arrayOfUsedOperants, operator) => { +let checkUpdateResult = (result, numbers, operators, arrayOfUsedoperands, operator) => { // Check the current operation base on the last result if (result == numbers[operators.indexOf(operator)] || result == numbers[operators.indexOf(operator) + 1]) { // When the current operation does affect the previous operation, update all the element already used in a calculation to the current result - // Also update the array of the index of operants already used + // Also update the array of the index of operands already used result = calculation(operator, numbers[operators.indexOf(operator)], numbers[operators.indexOf(operator) + 1]); - arrayOfUsedOperants.push(operators.indexOf(operator)); - arrayOfUsedOperants.push(operators.indexOf(operator) + 1); - for (let j = 0; j < arrayOfUsedOperants.length; j++) { - numbers[arrayOfUsedOperants[j]] = result; + arrayOfUsedoperands.push(operators.indexOf(operator)); + arrayOfUsedoperands.push(operators.indexOf(operator) + 1); + for (let j = 0; j < arrayOfUsedoperands.length; j++) { + numbers[arrayOfUsedoperands[j]] = result; } } else { // When the current operation does not affect the previous operation result, do not update all the element already used in a calculation to the current result @@ -68,21 +68,21 @@ let checkUpdateResult = (result, numbers, operators, arrayOfUsedOperants, operat operators[operators.indexOf(operator)] = ""; } -let calcOnOperator = (operator, result, numbers, operators, arrayOfUsedOperants, numberOfOccurence) => { +let calcOnOperator = (operator, result, numbers, operators, arrayOfUsedoperands, numberOfOccurence) => { if (numberOfOccurence != 0) { if (result == null) result = numbers[operators.indexOf(operator)]; for (let i = 0; i < numberOfOccurence; i++) { - checkUpdateResult(result, numbers, operators, arrayOfUsedOperants, operator); + checkUpdateResult(result, numbers, operators, arrayOfUsedoperands, operator); // Debugger // console.log("++++++++++++++++"); - // console.log(i + " " + operator + " iteration array of used indexes " + arrayOfUsedOperants); + // console.log(i + " " + operator + " iteration array of used indexes " + arrayOfUsedoperands); // console.log(i + " " + operator + " iteration operators = " + operators); // console.log(i + " " + operator + " iteration numbers = " + numbers); // console.log("++++++++++++++++"); } } - return numbers[arrayOfUsedOperants[arrayOfUsedOperants.length - 1]]; + return numbers[arrayOfUsedoperands[arrayOfUsedoperands.length - 1]]; }