جواب «Step 1: The Shape of an Item» چالش 1 از جلسهی 15
سؤال
قدم اول: شکل کالا و برچسب قیمتش.
- با interface شکل Item را بساز: name از نوع string و price از نوع number.
- تابع label یک Item میگیرد و متنی مثل Sword: 30 coins برمیگرداند.
- یک کالا به اسم sword با نوع Item بساز: اسم "Sword" و قیمت 30.
- label(sword) را نشان بده. برنامه باید Sword: 30 coins را بنویسد.
راهنما
در label: return item.name + ": " + item.price + " coins"; و آخر برنامه: const sword: Item = { name: "Sword", price: 30 }; و show(label(sword));
یک جواب درست
<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>
<pre id="out"></pre>
<script type="text/typescript">
interface Item {
name: string;
price: number;
}
function label(item: Item): string {
return item.name + ": " + item.price + " coins";
}
const sword: Item = { name: "Sword", price: 30 };
show(label(sword));
</script>
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن