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

Find the Red Lines چالش 5 از جلسه‌ی 19، سخت

این برنامه وضعیت یک صفحه را نشان می‌دهد: در حال بار شدن، آماده، یا خطا. نوع‌ها درست‌اند، ولی سه اشتباه در بقیه‌ی کد هست:
- در یک case فیلدی خوانده شده که آن حالت ندارد.
- برچسب یک case غلط نوشته شده.
- به یکی از حالت‌ها فیلدی داده شده که مال آن نیست.

برنامه باید Please wait...، Score: 18 و Error: no internet را نشان بدهد.

<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 Loading {
  kind: "loading";
}

interface Done {
  kind: "done";
  score: number;
}

interface Failed {
  kind: "failed";
  message: string;
}

type State = Loading | Done | Failed;

function view(state: State): string {
  switch (state.kind) {
    case "loading":
      return "Please wait...";
    case "done":
      return "Score: " + state.message;
    case "fail":
      return "Error: " + state.message;
  }
}

show(view({ kind: "loading" }));
show(view({ kind: "done", score: 18 }));
show(view({ kind: "failed", message: "no internet", score: 0 }));
</script>

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 19: برچسب روی هر شکل · چالش بعدی: Moves of a Fight

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