جواب «Colour the String» چالش 4 از جلسهی 21
سؤال
یک ریسه با پنج لامپ و یک آرایه با پنج رنگ داری.
با یک حلقهی for رنگ هر لامپ را عوض کن: لامپ شمارهی i رنگ شمارهی i را بگیرد (با style.backgroundColor).
راهنما
داخل حلقه بنویس bulbs[i].style.backgroundColor = colors[i]; و حلقه را تا i < colors.length ادامه بده.
یک جواب درست
<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
.lights { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin: 12px auto; }
.bulb { width: 34px; height: 34px; border-radius: 50%; background: #64748b; }
.bulb.on { background: #fde047; }
</style>
<div id="lights" class="lights">
<span class="bulb"></span>
<span class="bulb"></span>
<span class="bulb"></span>
<span class="bulb"></span>
<span class="bulb"></span>
</div>
<script>
let colors = ["red", "orange", "gold", "limegreen", "dodgerblue"];
let bulbs = document.querySelectorAll(".bulb");
for (let i = 0; i < colors.length; i++) {
bulbs[i].style.backgroundColor = colors[i];
}
</script>
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن