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

جواب «Find the Bug» چالش 5 از جلسه‌ی 39

سؤال

این دکمه باید بین light و dark جابه‌جا شود و یادش بماند. ولی سه مشکل دارد:
- یک خط قرمز (یک مقدار که در Theme نیست)،
- اسمِ save با اسمِ load یکی نیست،
- مقدار قبل از عوض شدن نگه داشته می‌شود.
هر سه را درست کن. اول برنامه: Theme: light. بعد از یک کلیک: Theme: dark، و بعد از باز کردن دوباره‌ی صفحه هم Theme: dark.

راهنما

"Light" باید "light" باشد. اسم باید "theme" باشد. و خط salam.save را ببر بعد از خطی که theme را عوض می‌کند.

یک جواب درست

<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()">Switch</button>
<pre id="out"></pre>

<script type="text/typescript">
type Theme = "light" | "dark";

let theme = salam.load<Theme>("theme", "light");

function press(): void {
  theme = theme === "light" ? "dark" : "light";
  salam.save("theme", theme);
  show("Theme: " + theme);
}

show("Theme: " + theme);
</script>

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