بتل کد Battle Codeدارد باز می‌شود…

Step 1: Rooms as Data چالش 1 از جلسه‌ی 30، آسان

صحنه و دکمه‌ها آماده‌اند، ولی فقط Flip all کد دارد (و بدون داده کار نمی‌کند). بقیه‌ی دکمه‌ها را در قدم‌های بعد می‌سازی.
- آرایه‌ی rooms را با چهار شیء بساز. هر شیء سه فیلد دارد: id (kitchen، bedroom، living، bath)، name و on. در داده، kitchen و living روشن باشند (on: true) و دو تای دیگر خاموش.
- تابع draw(room) را بنویس که یک اتاق را مثلِ داده‌اش کند، و drawAll() را که آن را برای همه صدا بزند.
- آخرِ کار drawAll(); را صدا بزن.

<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="b-kitchen" onclick="toggle('kitchen')">Kitchen</button>
<button id="b-bedroom" onclick="toggle('bedroom')">Bedroom</button>
<button id="b-living" onclick="toggle('living')">Living room</button>
<button id="b-bath" onclick="toggle('bath')">Bath</button>
<br>
<button id="off" onclick="allOff()">All off</button>
<button id="flip" onclick="flipAll()">Flip all</button>
<p id="msg"></p>

<script>
// 1. The data: const rooms = [ ... ];

// 2. function draw(room) { ... } and function drawAll() { ... }

// 3. Call drawAll();

function flipAll() {
  for (const room of rooms) {
    room.on = !room.on;
  }
  drawAll();
}

function toggle(id) {
  // step 2
}

function allOff() {
  // step 3
}
</script>

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 30: پروژه: خانه‌ی هوشمند · چالش بعدی: Step 2: A Button per Room

بیشتر بخوان: جاوااسکریپت چیست؟ از دکمه‌ی وب تا Node.js