Light N Bulbs چالش 5 از جلسهی 26، سخت
یک ریسه با ۸ لامپ داریم و دو دکمه: lightUp(3) و lightUp(6).
تابع lightUp(n) را بنویس:
- اول همهی لامپها را خاموش کند.
- بعد n لامپ اول را روشن کند.
پس اگر اول ۶ و بعد ۳ را بزنی، فقط ۳ لامپ روشن میماند.
<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>
<span class="bulb"></span>
<span class="bulb"></span>
<span class="bulb"></span>
</div>
<button id="three" onclick="lightUp(3)">3</button>
<button id="six" onclick="lightUp(6)">6</button>
<script>
let bulbs = document.querySelectorAll(".bulb");
// write the function lightUp(n) here
</script>
راهنما و جواب این چالش · برگشت به درس جلسهی 26: تابع با ورودی · چالش بعدی: Find the Bugs
بیشتر بخوان: جاوااسکریپت چیست؟ از دکمهی وب تا Node.js