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

Profile Viewer چالش 6 از جلسه‌ی 58، سخت

این صفحه پروفایل‌ها را با شماره نشان می‌دهد و «سرور» ساختگی serverGet(sessionUser, id) فقط می‌پرسد شماره وجود دارد یا نه: کاربر omid می‌تواند شماره‌ی 1 را بزند و ایمیل سارا را ببیند. serverGet را درست کن: اگر پروفایل نبود null برگرداند (مثل الان)، و اگر profile.owner با sessionUser فرق داشت هم null. تابع show از قبل درست است: برای null می‌نویسد not available.

<p>Logged in as: <b id="who">omid</b></p>
<input id="pid" placeholder="profile number">
<button id="go" onclick="show()">Show</button>
<p id="out">-</p>

<script>
const PROFILES = {
  1: { owner: "sara", email: "sara@example.test" },
  2: { owner: "omid", email: "omid@example.test" },
};
function serverGet(sessionUser, id) {
  const profile = PROFILES[id];
  if (!profile) return null;
  return profile;                       // BUG: never checks the owner
}
function show() {
  const sessionUser = document.getElementById("who").textContent;
  const id = document.getElementById("pid").value;
  const profile = serverGet(sessionUser, id);
  document.getElementById("out").textContent = profile ? profile.email : "not available";
}
</script>

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 58: اشتباه‌های رایج توسعه‌دهنده ۲: ده خطر اصلی وب · چالش بعدی: Typosquat Detector

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