جواب «Two Inputs» چالش 4 از جلسهی 26
سؤال
میخواهیم هر اتاق را رنگ کنیم. دکمهها آمادهاند و paint را با دو ورودی صدا میزنند: paint('kitchen', 'tomato') و paint('bath', 'skyblue').
تابع paint(id, color) را بنویس: اتاق را پیدا کند و style.backgroundColor آن را برابر color بگذارد.
راهنما
دو پارامتر را با , جدا کن: function paint(id, color) { و داخلش بنویس room.style.backgroundColor = color;
یک جواب درست
<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="p1" onclick="paint('kitchen', 'tomato')">Kitchen red</button>
<button id="p2" onclick="paint('bath', 'skyblue')">Bath blue</button>
<script>
function paint(id, color) {
let room = document.getElementById(id);
room.style.backgroundColor = color;
}
</script>
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن