From 9fdaf3b37611271d020c28d503b9ae8a59a469e0 Mon Sep 17 00:00:00 2001 From: nKiNk <39448774+nKiNk@users.noreply.github.com> Date: Thu, 14 Jan 2021 01:12:33 +0900 Subject: [PATCH 1/3] [FEAT] add practice, closure 3 --- .../practice_new/closure_part3_nkang.js | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 2_Scope_Closures/practice_new/closure_part3_nkang.js diff --git a/2_Scope_Closures/practice_new/closure_part3_nkang.js b/2_Scope_Closures/practice_new/closure_part3_nkang.js new file mode 100644 index 0000000..f9aa7ec --- /dev/null +++ b/2_Scope_Closures/practice_new/closure_part3_nkang.js @@ -0,0 +1,100 @@ +function calculator() { + let strNum = ""; + let leftNum = 0; + let rightNum = 0; + let oprt = ""; + + return function pressKey(key) { + if (/\d/.test(key)) { + strNum += key; + return key; + } + else if (/[+*/-]/.test(key) && oprt == "") { + leftNum = strNum ? Number(strNum) : leftNum; + strNum = ""; + oprt = key; + return key; + } + else if (/[+*/-]/.test(key) && oprt != "") { + leftNum = fourRules(leftNum, oprt, Number(strNum)); + strNum = ""; + oprt = key; + return key; + } + else if (key == "=") { + leftNum = fourRules(leftNum, oprt, Number(strNum)); + strNum = ""; + oprt = ""; + return leftNum; + } + } +} + +function fourRules(num1, oprt, num2) { + if (oprt == "+") return num1 + num2; + else if (oprt == "-") return num1 - num2; + else if (oprt == "*") return num1 * num2; + else if (oprt == "/") return num1 / num2; + else return "err"; +} + +var calc = calculator(); + +function useCalc(calc,keys) { + return [...keys].reduce( + function showDisplay(display,key){ + var ret = String( calc(key) ); + return ( + display + + ( + (ret != "" && key == "=") ? + "=" : + "" + ) + + ret + ); + }, + "" + ); +} + +function formatTotal(display) { + if (Number.isFinite(display)) { + // constrain display to max 11 chars + let maxDigits = 11; + // reserve space for "e+" notation? + if (Math.abs(display) > 99999999999) { + maxDigits -= 6; + } + // reserve space for "-"? + if (display < 0) { + maxDigits--; + } + + // whole number? + if (Number.isInteger(display)) { + display = display + .toPrecision(maxDigits) + .replace(/\.0+$/,""); + } + // decimal + else { + // reserve space for "." + maxDigits--; + // reserve space for leading "0"? + if ( + Math.abs(display) >= 0 && + Math.abs(display) < 1 + ) { + maxDigits--; + } + display = display + .toPrecision(maxDigits) + .replace(/0+$/,""); + } + } + else { + display = "ERR"; + } + return display; +} \ No newline at end of file From 7ac88d0634efe9245e486ce472cea0ccdf758798 Mon Sep 17 00:00:00 2001 From: nKiNk <39448774+nKiNk@users.noreply.github.com> Date: Sun, 17 Jan 2021 23:46:37 +0900 Subject: [PATCH 2/3] [FEAT] add practice for module got an TypeError `calc[fn] is not a function` --- .../practice_new/closure_part4_nkang.js | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 2_Scope_Closures/practice_new/closure_part4_nkang.js diff --git a/2_Scope_Closures/practice_new/closure_part4_nkang.js b/2_Scope_Closures/practice_new/closure_part4_nkang.js new file mode 100644 index 0000000..e87f506 --- /dev/null +++ b/2_Scope_Closures/practice_new/closure_part4_nkang.js @@ -0,0 +1,122 @@ +function calculator() { + let strNum = ""; + let leftNum = 0; + let oprt = ""; + + let publicAPI = { + num, + eq, + plus() { return operator("+"); }, + minus() { return operator("-"); }, + mult() { return operator("*"); }, + div() { return operator("/"); } + }; + + return publicAPI; + + function num(key) { + if (/\d/.test(key)) { + strNum += key; + return key; + } + } + + function eq() { + if (key == "=") { + leftNum = fourRules(leftNum, oprt, Number(strNum)); + strNum = ""; + oprt = ""; + return leftNum; + } + } + + function operator(key) { + if (oprt == "") { + leftNum = strNum ? Number(strNum) : leftNum; + } + else if (oprt != "") { + leftNum = fourRules(leftNum, oprt, Number(strNum)); + } + strNum = ""; + oprt = key; + return key; + } + + function fourRules(num1, oprt, num2) { + if (oprt == "+") return num1 + num2; + else if (oprt == "-") return num1 - num2; + else if (oprt == "*") return num1 * num2; + else if (oprt == "/") return num1 / num2; + else return "err"; + } +} + +var calc = calculator(); + +function useCalc(calc, keys) { + var keyMappings = { + "+": "plus", + "-": "minus", + "*": "mult", + "/": "div", + "=": "eq" + }; + + return [...keys].reduce( + function showDisplay(display, key) { + var fn = keyMappings[key] || "number"; + var ret = String(calc[fn](key)); + return ( + display + + ( + (ret != "" && key == "=") ? + "=" : + "" + ) + + ret + ); + }, + "" + ); +} + +function formatTotal(display) { + if (Number.isFinite(display)) { + // constrain display to max 11 chars + let maxDigits = 11; + // reserve space for "e+" notation? + if (Math.abs(display) > 99999999999) { + maxDigits -= 6; + } + // reserve space for "-"? + if (display < 0) { + maxDigits--; + } + + // whole number? + if (Number.isInteger(display)) { + display = display + .toPrecision(maxDigits) + .replace(/\.0+$/, ""); + } + // decimal + else { + // reserve space for "." + maxDigits--; + // reserve space for leading "0"? + if ( + Math.abs(display) >= 0 && + Math.abs(display) < 1 + ) { + maxDigits--; + } + display = display + .toPrecision(maxDigits) + .replace(/0+$/, ""); + } + } + else { + display = "ERR"; + } + return display; +} \ No newline at end of file From 7598a7c75de2e2ab0eac9f462bb05d8fdd672dcd Mon Sep 17 00:00:00 2001 From: nKiNk <39448774+nKiNk@users.noreply.github.com> Date: Mon, 18 Jan 2021 15:40:42 +0900 Subject: [PATCH 3/3] [FIX] change num to number num to number, eq() to eq(key) --- 2_Scope_Closures/practice_new/closure_part4_nkang.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/2_Scope_Closures/practice_new/closure_part4_nkang.js b/2_Scope_Closures/practice_new/closure_part4_nkang.js index e87f506..4d539b4 100644 --- a/2_Scope_Closures/practice_new/closure_part4_nkang.js +++ b/2_Scope_Closures/practice_new/closure_part4_nkang.js @@ -4,7 +4,7 @@ function calculator() { let oprt = ""; let publicAPI = { - num, + number, eq, plus() { return operator("+"); }, minus() { return operator("-"); }, @@ -14,14 +14,14 @@ function calculator() { return publicAPI; - function num(key) { + function number(key) { if (/\d/.test(key)) { strNum += key; return key; } } - function eq() { + function eq(key) { if (key == "=") { leftNum = fourRules(leftNum, oprt, Number(strNum)); strNum = ""; @@ -119,4 +119,4 @@ function formatTotal(display) { display = "ERR"; } return display; -} \ No newline at end of file +}