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

جواب «Step 4: The Defender Certificate» چالش 4 از جلسه‌ی 64

سؤال

صفحه‌ی گواهی را بساز: ده چک‌باکس (input type="checkbox" با کلاس item و شناسه‌های c1 تا c10) برای ده مورد چک‌لیست، یک کادر #name، یک دکمه‌ی #issue و یک بخش #cert. با کلیک روی #issue: اگر همه‌ی ده تا تیک خورده‌اند و نام خالی نیست، متن #cert را Web Defender: <name> (10/10) کن؛ وگرنه #cert → <تعداد تیک‌خورده>/10 - not yet. از document.querySelectorAll(".item:checked").length استفاده کن.

راهنما

ده <label><input type="checkbox" class="item" id="c1"> ...</label>. چک‌کننده خودش تیک‌ها را با کلیک می‌زند.

یک جواب درست

<h1>Web Defender checklist</h1>
<label><input type="checkbox" class="item" id="c1"> 1. Every input is validated</label><br>
<label><input type="checkbox" class="item" id="c2"> 2. No innerHTML with user text</label><br>
<label><input type="checkbox" class="item" id="c3"> 3. Links and images use an allow-list</label><br>
<label><input type="checkbox" class="item" id="c4"> 4. Passwords hashed with salt, compared safely</label><br>
<label><input type="checkbox" class="item" id="c5"> 5. No secrets in code or git</label><br>
<label><input type="checkbox" class="item" id="c6"> 6. Ownership checked before data is returned</label><br>
<label><input type="checkbox" class="item" id="c7"> 7. Queries use parameters</label><br>
<label><input type="checkbox" class="item" id="c8"> 8. Changes use POST with a CSRF token</label><br>
<label><input type="checkbox" class="item" id="c9"> 9. Generic errors, clean logs</label><br>
<label><input type="checkbox" class="item" id="c10"> 10. Lockout and session expiry</label><br>
<input id="name" placeholder="your name">
<button id="issue" onclick="issue()">Issue certificate</button>
<h2 id="cert">-</h2>

<script>
function issue() {
  const ticked = document.querySelectorAll(".item:checked").length;
  const name = document.getElementById("name").value;
  const cert = document.getElementById("cert");
  if (ticked === 10 && name.trim() !== "") {
    cert.textContent = "Web Defender: " + name + " (10/10)";
  } else {
    cert.textContent = ticked + "/10 - not yet";
  }
}
</script>

اول خودت امتحان کن؛ جواب‌های دیگر هم قبول است. برگرد و خودت حلش کن