πŸ– 쏙쏙 λ“€μ–΄μ˜€λŠ” ν•¨μˆ˜ν˜• μ½”λ”© - μ‹¬ν”Œν•œ μ½”λ“œλ‘œ λ³΅μž‘ν•œ μ†Œν”„νŠΈμ›¨μ–΄ 길듀이기

이 글은 쏙쏙 λ“€μ–΄μ˜€λŠ” ν•¨μˆ˜ν˜• μ½”λ”© - μ‹¬ν”Œν•œ μ½”λ“œλ‘œ λ³΅μž‘ν•œ μ†Œν”„νŠΈμ›¨μ–΄ 길듀이기λ₯Ό 읽고 μž‘μ„±ν•œ κΈ€μž…λ‹ˆλ‹€. ν•¨μˆ˜ν˜•λ„ ν•¨μˆ˜ν˜•μ΄μ§€λ§Œ, 이 책을 톡해 쒀더 κΉ”λ”ν•˜κ³  직관적인 μ½”λ“œλ₯Ό μž‘μ„±ν•  수 μžˆμ„ κ²ƒμ΄λž€ νŒ€μ›λ“€μ˜ μ˜κ²¬μ„ ν† λŒ€λ‘œ 이번 μŠ€ν„°λ”” μ„œμ μœΌλ‘œ μ„ μ •ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

Chap 4. μ•‘μ…˜μ—μ„œ 계산 λΉΌλ‚΄κΈ°

  • ν•¨μˆ˜λ‘œ 정보가 μ–΄λ–»κ²Œ λ“€μ–΄κ°€κ³  λ‚˜μ˜€λŠ”μ§€ μ΄ν•΄ν•˜κΈ°
  • ν…ŒμŠ€νŠΈν•˜κΈ° 쉽고 μž¬μ‚¬μš©μ„±μ΄ 쒋은 μ½”λ“œλ₯Ό λ§Œλ“€κΈ° μœ„ν•œ ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ° μ΄ν•΄ν•˜κΈ°
  • μ•‘μ…˜μ—μ„œ 계산을 λΆ„λ¦¬ν•˜λŠ” 방법 μ•ŒκΈ°

1) λ³€κ²½ μ „ μ½”λ“œ

let shoppingCart = []; // μ•‘μ…˜(A): μ „μ—­λ³€μˆ˜λŠ” λ³€κ²½ κ°€λŠ₯ν•˜κΈ° λ•Œλ¬Έ
let shoppingCartTotal = 0; // μ•‘μ…˜(A): μ „μ—­λ³€μˆ˜λŠ” λ³€κ²½ κ°€λŠ₯ν•˜κΈ° λ•Œλ¬Έ

const addItemToCart = (name, price) => {
  shoppingCart.push({ name, price }); // μ•‘μ…˜(A): μ „μ—­λ³€μˆ˜λ₯Ό λ³€κ²½ν•˜κ³  있음
  calcCartTotal();
};

const updateShippingIcons = () => {
  let buyButtons = getBuyButtonsDom(); // μ•‘μ…˜(A): DOM을 μ½μ–΄μ˜€κ³  있음
  for (let i = 0; i < buyButtons.length; i++) {
    let button = buyButtons[i];
    let item = button.item;
    if (item.price + shoppingCartTotal >= 20) {
      button.showFreeShippingIcon(); // μ•‘μ…˜(A): DOM을 λ³€κ²½ν•˜κ³  있음
    } else {
      button.hideFreeShippingIcon(); // μ•‘μ…˜(A): DOM을 λ³€κ²½ν•˜κ³  있음
    }
  }
};

const updateTaxDom = () => {
  setTaxDom(shoppingCartTotal * 0.1); // μ•‘μ…˜(A): DOM을 λ³€κ²½ν•˜κ³  있음
};

const calcCartTotal = () => {
  shoppingCartTotal = 0; // μ•‘μ…˜(A): μ „μ—­λ³€μˆ˜λ₯Ό λ³€κ²½ν•˜κ³  있음
  for (let i = 0; i < shoppingCart.length; i++) {
    shoppingCartTotal += shoppingCart[i].price;
  }
  updateTotalDom(); // μ•‘μ…˜(A): DOM을 λ³€κ²½ν•˜κ³  있음
  updateShippingIcons(); // μ•‘μ…˜(A): DOM을 λ³€κ²½ν•˜κ³  있음
  updateTaxDom(); // μ•‘μ…˜(A): DOM을 λ³€κ²½ν•˜κ³  있음
};

2) λͺ…μ‹œμ  μž…λ ₯/좜λ ₯, 암묡적 μž…λ ₯/좜λ ₯

  • λͺ…μ‹œμ  μž…λ ₯: ν•¨μˆ˜μ˜ 인자둜 μ „λ‹¬λ˜λŠ” κ°’
  • λͺ…μ‹œμ  좜λ ₯: ν•¨μˆ˜μ˜ λ°˜ν™˜κ°’
  • 암묡적 μž…λ ₯: μ „μ—­λ³€μˆ˜λ₯Ό μ½λŠ” λ“± 인자 μ™Έμ˜ λ‹€λ₯Έ λͺ¨λ“  μž…λ ₯κ°’
  • 암묡적 좜λ ₯: console.log(), DOM λ³€κ²½, μ „μ—­λ³€μˆ˜ λ³€κ²½ λ“± λ°˜ν™˜κ°’ μ™Έμ˜ λ‹€λ₯Έ λͺ¨λ“  좜λ ₯κ°’

암묡적 μž…λ ₯κ³Ό 좜λ ₯: λΆ€μˆ˜νš¨κ³Ό

3) λ³€κ²½ 절차

  1. 계산에 ν•΄λ‹Ήν•˜λŠ” μ½”λ“œλ₯Ό λΆ„λ¦¬ν•œλ‹€.
  2. 계산에 ν•΄λ‹Ήν•˜λŠ” μ½”λ“œλ₯Ό ν•¨μˆ˜λ‘œ μΆ”μΆœν•œλ‹€.
    • (μ²˜μŒλΆ€ν„° 이것저것 λ³€κ²½ν•˜λ €ν•˜μ§€ 말고 κ·Έλƒ₯ κ·ΈλŒ€λ‘œ ν•¨μˆ˜λ₯Ό λΆ„λ¦¬ν•˜κΈ°λ§Œ ν•œλ‹€.)
  3. ν•΄λ‹Ή ν•¨μˆ˜λ₯Ό κ·ΈλŒ€λ‘œ κΈ°μ‘΄ μ½”λ“œμ—μ„œ ν˜ΈμΆœν•œλ‹€.
  4. μΆ”μΆœν•œ ν•¨μˆ˜μ—μ„œ μž…λ ₯값은 인자둜, 좜λ ₯값은 λ¦¬ν„΄κ°’μœΌλ‘œ λ³€κ²½ν•œλ‹€.

3-1) λ³€κ²½ μ „ calcCartTotal() ν•¨μˆ˜

const calcCartTotal = () => {
  let shoppingCartTotal = 0; // μ•‘μ…˜(A): μ „μ—­λ³€μˆ˜λ₯Ό λ³€κ²½ν•˜κ³  있음
  for (let i = 0; i < shoppingCart.length; i++) {
    shoppingCartTotal += shoppingCart[i].price;
  }
  updateTotalDom(); // μ•‘μ…˜(A): DOM을 λ³€κ²½ν•˜κ³  있음
  updateShippingIcons(); // μ•‘μ…˜(A): DOM을 λ³€κ²½ν•˜κ³  있음
  updateTaxDom(); // μ•‘μ…˜(A): DOM을 λ³€κ²½ν•˜κ³  있음
};

3-2) λ³€κ²½ ν›„ calcCartTotal() ν•¨μˆ˜

const calcTotal = (cart) => {
  let total = 0;
  for (let i = 0; i < cart.length; i++) {
    total += cart[i].price;
  }
  return total;
};

const calcCartTotal = () => {
  let total = calcTotal(shoppingCart);

  updateTotalDom(); // μ•‘μ…˜(A): DOM을 λ³€κ²½ν•˜κ³  있음
  updateShippingIcons(); // μ•‘μ…˜(A): DOM을 λ³€κ²½ν•˜κ³  있음
  updateTaxDom(); // μ•‘μ…˜(A): DOM을 λ³€κ²½ν•˜κ³  있음
};

Tip) 인자둜 λ°›λŠ” 객체λ₯Ό λΆˆλ³€μ„±μ„ μœ μ§€ν•΄μ•Όν•œλ‹€. 그렇지 μ•ŠμœΌλ©΄ 인자라 해도 λ°”κΉ₯에 μ‘΄μž¬ν•˜λŠ” 값은 λ³€κ²½ν•  수 있기 λ•Œλ¬Έμ΄λ‹€.
주둜 μΉ΄ν”Ό-온-라이트(copy-on-write) λ°©λ²•μœΌλ‘œ 객체λ₯Ό λ³΅μ‚¬ν•΄μ„œ μ‚¬μš©ν•œλ‹€.

4) λ³€κ²½ ν›„ μ½”λ“œ

let shoppingCart = [];
let shoppingCart = 0;

const addItemToCart = ({ cart, name, price }) => {
  addItem({ cart, name, price });
  calcCartTotal();
};

const addItem = ({ cart, name, price }) => {
  cart.push({ name, price });
};

const updateShippingIcons = () => {
  let buyButtons = getBuyButtonsDom();
  for (let i = 0; i < buyButtons.length; i++) {
    let button = buyButtons[i];
    let item = button.item;
    if (isOverTwentyDollars({ item, shoppingCartTotal })) {
      button.showFreeShippingIcon();
    } else {
      button.hideFreeShippingIcon();
    }
  }
};

const isOverTwentyDollars = ({ item, shoppingCartTotal }) => {
  const newItem = { ...item };
  return newItem.price + shoppingCartTotal >= 20;
};

const updateTaxDom = () => {
  setTaxDom(calcTax(shoppingCartTotal));
};

const calcTax = (cartTotal) => {
  return cartTotal * 0.1;
};

const calcCartTotal = () => {
  const shoppingCartTotal = calcTotal(shoppingCart);
  updateTotalDom();
  updateShippingIcons();
  updateTaxDom();
};

const calcTotal = ({ cart }) => {
  let total = 0;
  for (let i = 0; i < cart.length; i++) {
    total += cart[i].price;
  }
  return total;
};