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 all 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 = {
number,
eq,
plus() { return operator("+"); },
minus() { return operator("-"); },
mult() { return operator("*"); },
div() { return operator("/"); }
};

return publicAPI;

function number(key) {
if (/\d/.test(key)) {
strNum += key;
return key;
}
}

function eq(key) {
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;
}