unit converter ěěą
underdash ě˝ë 댏롰 ě 댏(1)
ě´ě ęłľëśěě ě´ě´ě
minutsToHours ěť´íŹëí¸ě ëěźí ęľŹěĄ°ëĄ ë§ë¤ěë¤.
íľěŹě inputě valueę° ëłí ëë§ë¤ amountę° ě
ë°ě´í¸ë¨ęłź ëěě ěť´íŹëí¸ę° 댏ë ëë§ ëë¤ë ę˛ě´ë¤.
const KmToMiles = () => {
const [amount, setAmount] = React.useState(0);
const [flipped, setFlipped] = React.useState(false);
const onChange = (event) => setAmount(event.target.value);
const reset = () => setAmount(0);
const onFlip = () => {
setFlipped((current) => !current);
reset();
};
return (
<div>
<div>
<label htmlFor="km">Km</label>
<input
value={!flipped ? amount : amount * 1.609}
id="km"
placeholder="Km"
type="number"
onChange={onChange}
disabled={flipped}
/>
</div>
<div>
<label htmlFor="miles">Miles</label>
<input
value={flipped ? amount : amount / 1.609}
id="miles"
placeholder="Miles"
type="number"
onChange={onChange}
disabled={!flipped}
/>
</div>
<button onClick={reset}>Reset</button>
<button onClick={onFlip}>Flip</button>
</div>
);
};
const App = () => {
return (
<div>
<select>
<option value="select">Select unit converter</option>
<option value="0">Minutes & Hours</option>
<option value="1">Km & Miles</option>
</select>
</div>
);
};
ě íí optioně value ę°ě ě ěĽíë index state뼟 ěěąíë¤.
ě´ëĽź selectě valueě í ëšíë¤.
기본ę°ě ěěëĄ "select"ëĄ ě¤ě íë¤.(ě í ěë´ ëŹ¸ęľŹ)
const App = () => {
const [index, setIndex] = React.useState("select");
return (
<div>
<select value={index}>
<option value="select">Select unit converter</option>
<option value="0">Minutes & Hours</option>
<option value="1">Km & Miles</option>
</select>
</div>
);
};
selectěě change ě´ë˛¤í¸ę° ë°ěí늴 index뼟 ě íë optioně value ę°ěźëĄ ě ë°ě´í¸ íęł ë°ë ë°ě´í°ëĽź ë°íěźëĄ App ěť´íŹëí¸ëĽź 댏ë ëë§íë¤.
ě´ë ě
ë°ě´í¸ë indexě ę°ě ë°ëź ë¤ëĽ¸ ë¨ě ëłí기뼟 ëł´ěŹěŁźëëĄ íë¤.
JSX 돸ë˛ěěë { }
ěě ěë°ě¤íŹëŚ˝í¸ ě˝ë뼟 ě¸ ě ěë¤.
const App = () => {
const [index, setIndex] = React.useState("select");
const onSelect = () => {
setIndex(event.target.value); // optioně value
};
return (
<div>
<h1>Super Converter</h1>
<select value={index} onChange={onSelect}>
<option value="select">Select unit converter</option>
<option value="0">Minutes & Hours</option>
<option value="1">Km & Miles</option>
</select>
{index === "select" ? <h4>Please select unit converter</h4> : null}
{index === "0" ? <MinutesToHours /> : null}
{index === "1" ? <KmToMiles /> : null}
</div>
);
};
const root = document.getElementById("root");
ReactDOM.render(<App />, root);
MDN ěŹě´í¸ ě°¸ęł
if/else ě°ëŹě ě°ě§ ë§ęł if/return 꾏쥰뼟 ěŹěŠí ę˛
돴쥰깴 ě˝ëě 길ě´ëĽź ě¤ě´ë ę˛ëł´ë¤ ę°ë
ěąě´ ë ě¤ěíë¤.
ex. ěźí 쥰깴 ě°ě°ě ëëëĄ í ë˛ě íëë§ ěŹěŠí ę˛
return ěěě
íšě ëŹ¸ë§Ľě´ ë°ë ë ě¤ ę°ę˛Šě ëě°ëëĄ íë¤.
ex. ëłě뼟 ě ě ě¸íęł í¨ě뼟 ě ě¸í기 ě ě ě¤ ę°ę˛Šě ëě´ë¤.
_.each = function (collection, iterator) {
if (Array.isArray(collection)) {
for (let i = 0; i < collection.length; i++) {
iterator(collection[i], i, collection);
}
} else {
for (key in collection) {
iterator(collection[key], key, collection);
❠꾏í 쥰깴: ë°°ě´ ëŠěë(ex. forEach, map ëą) ěŹěŠ X
for...in 돸ě ě ěę° ěë
ě´ëŚě ę°ě§ ěěąęłź ěěë
모ë ě´ęą° ę°ëĽí ěěąë¤ě ë°ííë¤.
ë°ëźě, for...in 돸ě ě¸ëąě¤ě ěěę° ě¤ěí ë°°ě´ěěë ë°ëłľě ěí´ ěŹěŠí ě ěěźëŠ°
ëěě´ ë°°ě´ě´ ěë ę°ě˛´ëź íëëźë
for...in 돸ě ěŹěŠí늴 ěëí ë°ě ë¤ëĽ¸ ę˛°ęłźę° ëěśë ě ěë¤.
// ěě
const collection = [1, 2, 3];
Array.prototype.name = "collection"; // íëĄí íě
ěěą(ě´ě ě ěę° ěë ěěą)
collection.next = "four"; // ě ěę° ěë ěěą
for (const key in collection) {
console.log(key); // 0 1 2 ëż ěëëź next name ë ěśë Ľë¨
}
ę°ě˛´ě íëĄí íě
ě´ ěë ę°ě˛´ ě체ě ě°ę˛°ë ěěąë§ ęł ë ¤íęł ě íë¤ëŠ´(ěěë ěěą ęł ë ¤ X)
for...in 돸곟 í¨ęť Object.hasOwnProperty()
뼟 ěŹěŠí´ěź íë¤.
Object.hasOwnProperty() ëŠěëë ę°ě˛´ę° íšě íëĄíźí°ëĽź ě기ë§ě ě§ě ě ě¸ íëĄíźí°ëĄě ěě íęł ěëě§
뼟 íë¨íëë° ěŹěŠëë¤.
for (const key in collection) {
if (collection.hasOwnProperty(key)) {
console.log(key); // 0 1 2 next (íëĄí íě
ěěą nameě ěśë Ľëě§ ěëë¤)
}
}
íšě for...in & Object.hasOwnProperty() ëě Object.keys()
뼟 ě´ěŠíěŹ ë¤ěęłź ę°ě´ ěěąí ěë ěë¤.
Object.keys(collection).forEach(key => console.log(key));
// 0 1 2 next (ë§ě°Źę°ě§ëĄ íëĄí íě
ěěą nameě ěśë Ľëě§ ěëë¤)
ë°°ě´ě ë°ëłľě ěí´ěë for...of뼟 ěŹěŠíë ę˛ě ěśě˛íë¤.
ëłěě íëĄíźí°ę° ë´ę¸°ë for...inęłź ëŹëŚŹ for...of 돸ěěë íëĄíźí°ě ę°ě´ ë´ę¸´ë¤.
íëĄí íě
ěěě ěí íëĄíźí°ě, ë°°ě´ě ě체 ě¸ëąě¤ëĽź ě ě¸íęł ěěëĄ ěśę°í íëĄíźí° 모ë 돴ěëë¤.
const collection = [1, 2, 3];
Array.prototype.name = "collection";
collection.next = "four";
for (const value of collection) {
console.log(value); // 1 2 3 (collectionęłź fourě ěśë Ľëě§ ěëë¤)
}
for...of 돸곟 Array.entries()
뼟 ě´ěŠíěŹ ëŹ¸ě ě˝ëě ë°°ě´ ëśëśě ë¤ěęłź ę°ě´ ęł ěłëłź ěë ěë¤.
if (Array.isArray(collection)) {
for (let i = 0; i < collection.length; i++) {
iterator(collection[i], i, collection);
}
}
// ęľłě´ ěëě˛ëź ě¸ íěë ěě§ë§, ě ěëë ę°ě ě˝ëě´ë¤
if (Array.isArray(collection)) {
for (const [index, value] of collection.entries()) {
iterator(value, index, collection);
}
}
íí¸, for...ofë Object ę°ě˛´ëĽź ëěěźëĄë ěŹěŠí ě ěë¤.
Object ę°ě˛´ě for...of 돸ě ěŹěŠ
í기 ěí´ěë Object.keys()뼟 ě´ěŠí´ ę°ě˛´ě íëĄíźí°ë¤ëĄ ě´ëŁ¨ě´ě§ ë°°ě´ě 꾏í í í´ëš ë°°ě´ě for...of 돸ě ěŹěŠí´ěź íë¤.
const keys = Object.keys(obj); // ["name", "age"]
for (const key of keys) {
console.log(key, obj[key]); // name syong age 10
}
ë°ëźě, 돸ě ě˝ëë ë¤ěęłź ę°ě´ ěě í ě ěë¤.
_.each = function (collection, iterator) {
if (Array.isArray(collection)) {
for (let i = 0; i < collection.length; i++) {
iterator(collection[i], i, collection);
}
} else {
for (key in collection) {
if (collection.hasOwnProperty(key)) { // ěśę° â
iterator(collection[key], key, collection);
}
}
}
í¨ěë í ě ěë¤ëŠ´ ěľëí ěŹíěŠ íë ę˛ě´ ě˘ë¤.
ě˛ěě ěěąí ě˝ë
_.filter = function (collection, test) {
let newArr = [];
for (let i = 0; i < collection.length; i++) {
if (test(collection[i])) {
newArr.push(collection[i]);
}
}
return newArr;
};
_.each í¨ě뼟 ěŹíěŠíěŹ ěě í ě˝ë
_.filter = function (collection, test) {
const newArr = [];
_.each(collection, function (item) {
if (test(item)) newArr.push(item);
});
return newArr;
};
ě˛ěě ěěąí ě˝ë
_.reject = function (collection, test) {
let newArr = [];
for (let i = 0; i < collection.length; i++) {
if (!test(collection[i])) {
newArr.push(collection[i]);
}
}
return newArr;
};
_.each í¨ě뼟 ěŹíěŠíěŹ ěěąí ě˝ë
_.reject = function (collection, test) {
const newArr = [];
_.each(collection, function (item) {
if (!test(item)) newArr.push(item);
});
return newArr;
};
ě˛ěě ěěąí ě˝ë
_.reduce = function (collection, iterator, accumulator) {
if (accumulator !== 0 && accumulator === undefined) {
for (let i = 0; i < collection.length - 1; i++) {
if (i === 0) {
accumulator = iterator(collection[0], collection[1]);
continue;
}
accumulator = iterator(accumulator, collection[i + 1]);
}
} else {
for (key in collection) {
accumulator = iterator(accumulator, collection[key]);
}
}
return accumulator;
};
ę¸°ëł¸ę° ë§¤ę°ëłě
뼟 íěŠíěŹ ě˛ě ěěąí ě˝ë
_.reduce = function (collection, iterator, accumulator = collection[0]) {
const _collection =
accumulator === collection[0] ? collection.slice(1) : collection;
for (key in _collection) {
if (collection.hasOwnProperty(key)) {
accumulator = iterator(accumulator, _collection[key]);
}
}
return accumulator;
};
ę¸°ëł¸ę° ë§¤ę°ëłě뼟 ěŹěŠí늴ě _.contains
í¨ěě í
ě¤í¸ ěźě´ě¤ë íľęłźí기 ěí´ collectioně´ 'Object ę°ě˛´'ě¸ ę˛˝ě°ë ęł ë ¤í´ěź íë¤.
모ë í
ě¤í¸ ěźě´ě¤ëĽź íľęłźí긴 íě§ë§, accumulator === collection[0]
ëśëśě´ ë¤ëĽ¸ í
ě¤í¸ ěźě´ě¤ě ę˛˝ě° ěëŹëĽź ěźěźí¤ę˛ ëë¤.
_.reduce
í¨ě í¸ěś ě accumulatorëĄ ě ëŹí ę°ě´ collection[0]ęłź ę°ě 경ě°ěë _collection
ě collection.slice(1)ě´ ë기 ë돸ě´ë¤.
// í
ě¤í¸ ěźě´ě¤
const result = _.reduce(
[3, 2, 1],
function (memo, item) {
return memo - item;
},
3
);
expect(result).to.equal(-3); // ě´ě´ěź íëë° ě¤ě ëĄë 0ě´ ëě¨ë¤
ę¸°ëł¸ę° ë§¤ę°ëłěę° ěëëź ě§ě _.reduce
í¨ěě ě´ęšę°ě ě¤ě í 경ě°ěë ę¸°ëł¸ę° ë§¤ę°ëłě뼟 ěŹěŠí ę˛ě˛ëź ë°ěë¤ěŹě ¸ ěëŹę° ë°ěíë ę˛ě´ë¤.
결곟ě ěźëĄ ě´ë ě몝 ěěąë ě˝ëě´ë¤.
đââď¸ ęˇ¸ë°ë° ę¸°ëł¸ę° ë§¤ę°ëłě뼟 ěŹěŠíě ë accumulatorę° ę¸°ëł¸ę° ë§¤ę°ëłěě¸ě§
_.reduce
í¨ěě ě í´ě¤ ě´ęšę°ě¸ě§ íëłíë ë°Šë˛ě ëŞ¨ëĽ´ę˛ ë¤.
ę¸°ëł¸ę° ë§¤ę°ëłěë í¨ě í¸ěś ěě ě´ëŻ¸ ě¤ě ě´ ëëë° ě˝ë ěěě ě´ëťę˛ 꾏ëśě í´ěź íë 깸ęš.
ěľě˘ ě ěźëĄ ěě í ě˝ë
_.reduce = function (collection, iterator, accumulator) {
if (accumulator !== 0 && accumulator === undefined) {
accumulator = collection[0];
for (let i = 1; i < collection.length; i++) {
accumulator = iterator(accumulator, collection[i]);
}
} else {
for (key in collection) {
if (collection.hasOwnProperty(key)) {
accumulator = iterator(accumulator, collection[key]);
}
}
}
return accumulator;
};
ëę° íŹę˛ ë°ë ęą´ ěë¤.
for ë°ëłľëŹ¸ ë¸ëĄě ě´ě§ ě 댏í´ěŁźęł hasOwnProperty()뼟 ěśę°íë¤.