Code States
Front-end boost camp
Today
I
Learned
๐ฌ 42์ผ์ฐจ, ์์์ผ!
const { type } = require("express/lib/response");
/**
* 1. Browser์ ์กด์ฌํ๋ JSON.stringfy ํจ์๋ฅผ ์ง์ ๊ตฌํํด ๋ด
๋๋ค.
* JSON.stringfy ํจ์๋ input ๊ฐ์ JSON ํ์์ผ๋ก ๋ณํํฉ๋๋ค.
* ๋จ, undefined์ function์ JSON์ผ๋ก ์๋ต๋๊ฑฐ๋ null๋ก ๋ณํ๋ฉ๋๋ค.
*
* 2. stringfyJSON์ ์๋์ ๊ฐ์ด ์๋ํฉ๋๋ค.
* - Boolean์ด input์ผ๋ก ์ฃผ์ด์ก์ ๊ฒฝ์ฐ
* stringifyJSON(true); // 'true'
* - String์ด input์ผ๋ก ์ฃผ์ด์ก์ ๊ฒฝ์ฐ
* stringifyJSON('foo'); // '"foo"'
* - Array๊ฐ input์ผ๋ก ์ฃผ์ด์ก์ ๊ฒฝ์ฐ
* stringifyJSON([1, 'false', false]); // '[1,"false",false]'
* - Object๊ฐ input์ผ๋ก ์ฃผ์ด์ก์ ๊ฒฝ์ฐ
* stringifyJSON({ x: 5 }); // '{"x":5}'
* - undefined, function์ด ์ฃผ์ด์ก์ ๊ฒฝ์ฐ
* stringifyJSON(undefined) // undefined
* stringifyJSON(function(){}) // undefined
* stringifyJSON({ x: undefined, y: function(){} }) // '{}'
*
* 3. spec/stringifyJSONSpec.js์ stringifiableObjects ๋ฐฐ์ด์ ์ฐธ๊ณ ํด์ ํ
์คํธ์์ ์ด๋ค input ๊ฐ๋ค์ด
* ์ฃผ์ด์ง๊ณ , ์ด๋ป๊ฒ stringifyํด ์ฃผ์ด์ผ ํ ์ง ์๊ฐํด ๋ณด์ธ์.
*
* 4. ๊ทธ๋ฅ ํ
์คํธ ํต๊ณผ๋ฅผ ํ๊ณ ์ถ์ผ์๋ค๋ฉด, ๋ค์๊ณผ ๊ฐ์ด ๊ตฌํํ๋ฉด ๋ ๊ฑฐ์์.
* const stringifyJSON = JSON.stringify;
*
* ํ์ง๋ง ์ด ๊ณผ์ ์ ๋ชฉ์ ์ ์ฌ๊ท๋ฅผ ๊ณต๋ถํ๋ ๊ฒ์ด๋, ์ฒ์๋ถํฐ ๊ตฌํํด๋ด์ผ๊ฒ ์ง์?:
*/
function stringifyJSON(obj) {
// your code goes here
if (typeof obj === 'number' || typeof obj === 'boolean' || obj === null) {
return `${obj}`;
}
if (typeof obj === 'string') {
return `"${obj}"`;
}
if (Array.isArray(obj)) {
let result = "";
for (element of obj) {
result += stringifyJSON(element) + ',';
}
result = result.slice(0, result.length - 1);
return `[${result}]`;
}
if (typeof obj === 'object') {
let result = "";
for (key in obj) {
if (typeof obj[key] === 'function' || obj[key] === undefined) {
continue;
}
result += stringifyJSON(key) + ":" + stringifyJSON(obj[key]) + ",";
}
result = result.slice(0, result.length - 1);
return `{${result}}`
}
};
// ๋ค์ ์ฝ๋๋ ๊ฒฐ๊ณผ ์ ์ถ์ ์ํ ์ฝ๋์
๋๋ค. ์ ๊ฒฝ ์ฐ์ง ์์๋ ์ข์ต๋๋ค.
if (typeof window === "undefined") {
module.exports = stringifyJSON;
}
const root = document.getElementById('root');
function createTreeView(menu, currentNode) {
for (let i = 0; i < menu.length; i++) {
const li = document.createElement('li');
if (menu[i].children) {
const input = document.createElement('input');
input.type = 'checkbox';
const span = document.createElement('span');
span.textContent = menu[i].name;
const ul = document.createElement('ul');
li.append(input, span, ul);
currentNode.append(li);
createTreeView(menu[i].children, ul);
} else {
li.textContent = menu[i].name;
currentNode.append(li);
}
}
}
createTreeView(menu, root);
const arr = [1, 2, 3, 4, 5];
function arrSum(arr) {
if ( (1)__________ ) {
return 0;
}
return (2)__________ + arrSum( (3)__________ );
}
์ฌ๊ท.. 60ํผ์ผํธ ์ ๋ ์ดํด๋ ์ ๋๋๋ฐ... ์ด๋ค ์์น์ ์ด๋ป๊ฒ ์ ์ฌ์ ์์ ๋ฃ์ด์ผ ํ๋์ง ํ๋จ์ด ์กฐ๊ธ ์ด๋ ต๋ค.