Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] add practice for module #34

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions 2_Scope_Closures/practice_new/closure_part3_nkang.js
Original file line number Diff line number Diff line change
@@ -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;
}
122 changes: 122 additions & 0 deletions 2_Scope_Closures/practice_new/closure_part4_nkang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
function calculator() {
let strNum = "";
let leftNum = 0;
let oprt = "";

let publicAPI = {
num,
Copy link
Member

@hochan222 hochan222 Jan 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VM29:69 Uncaught TypeError: calc[fn] is not a function
at showDisplay (:69:32)
at Array.reduce ()
at useCalc (:66:20)
at eval (eval at (new-tab-page/:1), :1:1)
at :1:1

다음 Err는 num이 원인인것 같습니다.

function useCalc 에서

 var fn = keyMappings[key] || "number";
 var ret = String(calc[fn](key));

fn은 숫자일때 "number"로 인자로 들어갑니다. 하지만 함수안에는 number함수대신 num이있으므로 calc[fn] is not a function이 발생하는 걸로 추측됩니다!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엉엉엉엉 ㅠㅠ 감사합니다.

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 == "=") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(1)이 조건문을 지우고

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";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(2)"number"를 "num"으로 바꾸면 돌아갈 것 같습니다!!

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;
}