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

Step 3: Validation and Login چالش 3 از جلسه‌ی 60، متوسط

اشکال‌های ۳ و ۴. در post: اگر پیام (بعد از trim) خالی یا بلندتر از ۳۰ حرف است، چیزی اضافه نکن و #status را invalid message کن؛ وگرنه اضافه کن و #status → posted. در login: پیام خطا برای هر حالت غلط فقط wrong username or password؛ و بعد از ۳ تلاش غلط برای یک نام، حتی رمز درست هم account locked بگیرد (کاربران: sara/tiger22). ورود درست → welcome, <name>.

<h3>Class messenger</h3>
<input id="name" placeholder="name">
<input id="pw" type="password" placeholder="password">
<button id="go" onclick="login()">Log in</button>
<p id="auth">-</p>
<input id="msg" placeholder="message">
<button id="send" onclick="post()">Post</button>
<p id="status">-</p>
<ul id="list"></ul>

<script>
const USERS = { sara: "tiger22" };
function login() {
  const name = document.getElementById("name").value;
  const pw = document.getElementById("pw").value;
  const auth = document.getElementById("auth");
  if (!(name in USERS)) { auth.textContent = "no user named " + name; return; }   // bug 4a
  if (USERS[name] !== pw) { auth.textContent = "wrong password"; return; }       // bug 4a + 4b (no lockout)
  auth.textContent = "welcome, " + name;
}
function post() {
  const name = document.getElementById("name").value;
  const msg = document.getElementById("msg").value;
  const li = document.createElement("li");                                        // bug 3: no validation
  li.textContent = name + ": " + msg;
  document.getElementById("list").appendChild(li);
  document.getElementById("status").textContent = "posted";
}
</script>

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 60: پروژه: آسیب‌پذیری را ببند · چالش بعدی: Step 4: Ownership and Final Test

بیشتر بخوان: دیکشنری در پایتون: آموزش کامل کلید و مقدار با مثال