جواب «Find the Bugs» چالش 6 از جلسهی 28
سؤال
این کلید باید اتاق خواب را با هر بار زدن روشن و خاموش کند و بنویسد Bedroom is on یا Bedroom is off. ولی اصلاً کار نمیکند.
دو اشتباه دارد. هر دو را پیدا و درست کن. (راهنمایی: اولی کلِ برنامه را از کار انداخته؛ دومی بعد از درست شدنِ اولی پیدا میشود: کلید را دو بار بزن.)
راهنما
در خط اول بین "bedroom" و name یک , جا افتاده. و فیلد on است، نه On: room.on = !room.on;
یک جواب درست
<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="switch" onclick="press()">Switch</button>
<p id="msg"></p>
<script>
const room = { id: "bedroom", name: "Bedroom", on: false };
function draw() {
let box = document.getElementById(room.id);
let line = document.getElementById("msg");
if (room.on) {
box.classList.add("on");
line.textContent = `${room.name} is on`;
} else {
box.classList.remove("on");
line.textContent = `${room.name} is off`;
}
}
function press() {
room.on = !room.on;
draw();
}
</script>
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن