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

جواب «Find the Red Lines» چالش 5 از جلسه‌ی 17

سؤال

بازی دو تیم دارد: red و blue. این برنامه برای هر بازیکن می‌نویسد در کدام تیم است.
دو اشتباه دارد. پیدایشان کن و درست کن (نوع Team درست است، آن را عوض نکن):
- در تابع، سراغ تیمی رفته‌اند که وجود ندارد. آن if را پاک کن.
- تیم یکی از بازیکن‌ها غلط نوشته شده.

برنامه باید Sara plays for RED و Ali plays for BLUE را نشان بدهد.

راهنما

تیم "green" در Team نیست: آن if را با سه خطش پاک کن. و "bleu" باید "blue" باشد.

یک جواب درست

<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">
type Team = "red" | "blue";

interface Player {
  name: string;
  team: Team;
}

function cheer(p: Player): string {
  if (p.team === "red") {
    return p.name + " plays for RED";
  }
  return p.name + " plays for BLUE";
}

const sara: Player = { name: "Sara", team: "red" };
const ali: Player = { name: "Ali", team: "blue" };
show(cheer(sara));
show(cheer(ali));
</script>

اول خودت امتحان کن؛ جواب‌های دیگر هم قبول است. برگرد و خودت حلش کن