بتل کد Battle Codeدارد باز می‌شود…

The Shop Sells Too Much چالش 4 از جلسه‌ی 38، متوسط

این فروشگاه جاوااسکریپتی فقط ۲ ربات دارد، ولی بعد از سه کلیک می‌نویسد left: -1. شیء p شل است و یک غلط تایپی در آن پنهان شده.
۱. یک interface به اسم Product بساز: name (متن)، price (عدد) و stock (عدد).
۲. به buy نوع بده: ورودی Product، خروجی string.
۳. خط قرمز تازه باگ را نشان می‌دهد: درستش کن.
کلیک اول: Bought Robot, left: 1، کلیک دوم: Bought Robot, left: 0، کلیک سوم: Sold out.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<button id="go" onclick="press()">Buy</button>
<pre id="out"></pre>

<script type="text/typescript">
const product = { name: "Robot", price: 30, stock: 2 };

function buy(p) {
  if (p.stok === 0) {
    return "Sold out";
  }
  p.stock = p.stock - 1;
  return "Bought " + p.name + ", left: " + p.stock;
}

function press(): void {
  show(buy(product));
}
</script>

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 38: اسباب‌کشی، قدم دوم · چالش بعدی: Find the Red Lines

بیشتر بخوان: جاوااسکریپت چیست؟ از دکمه‌ی وب تا Node.js