Is It On? چالش 5 از جلسهی 27، سخت
دکمههای بالا اتاق را روشن و خاموش میکنند. دکمههای پایین میپرسند اتاق چه وضعی دارد و جوابِ status(id) را در #msg مینویسند.
دو تابع بنویس:
- isOn(id): اگر آن اتاق کلاس on دارد true و وگرنه false برگرداند.
- status(id): با کمکِ isOn، متنِ kitchen is on یا kitchen is off را برگرداند (به جای kitchen همان id).
<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
.house { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; max-width: 340px; margin: 12px auto; }
.room { width: 150px; padding: 18px 0; border-radius: 12px; background: #334155; color: white; }
.room.on { background: #fde047; color: black; }
</style>
<div class="house">
<div id="kitchen" class="room">Kitchen</div>
<div id="bedroom" class="room">Bedroom</div>
<div id="living" class="room">Living room</div>
<div id="bath" class="room">Bath</div>
</div>
<button id="flip-k" onclick="flip('kitchen')">Kitchen switch</button>
<button id="flip-b" onclick="flip('bath')">Bath switch</button>
<br>
<button id="ask-k" onclick="ask('kitchen')">Kitchen?</button>
<button id="ask-b" onclick="ask('bath')">Bath?</button>
<p id="msg"></p>
<script>
function flip(id) {
document.getElementById(id).classList.toggle("on");
}
function ask(id) {
document.getElementById("msg").textContent = status(id);
}
// write isOn(id) and status(id) here
</script>
راهنما و جواب این چالش · برگشت به درس جلسهی 27: تابعی که جواب میدهد · چالش بعدی: Find the Bugs
بیشتر بخوان: جاوااسکریپت چیست؟ از دکمهی وب تا Node.js