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

Make a Backup چالش 5 از جلسه‌ی 17، متوسط

قبل از بردن اپ به گوشی دیگر، از کلمه‌ها پشتیبان بگیر! تابع backup() را بنویس که آرایه‌ی words را با JSON.stringify به متن تبدیل کند و در کادر #backup بگذارد (با .value). این متن را می‌شود کپی کرد و جای دیگر برگرداند.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Word Book</title>
<style>
body { font-family: sans-serif; margin: 0; }
header { background-color: #1e88e5; color: white; padding: 12px 16px; }
.app { max-width: 420px; margin: 0 auto; padding: 16px; }
button, input, textarea { font-size: 16px; min-height: 44px; }
textarea { width: 100%; box-sizing: border-box; }
</style>
</head>
<body>
<div class="app">
  <h1>Word Book</h1>
  <form id="addForm" onsubmit="addWord(event)">
    <label for="word">New word</label>
    <input id="word" placeholder="star">
    <button>Add</button>
  </form>
  <ul id="words"></ul>
  <button id="backupBtn" onclick="backup()">Make a backup</button>
  <p><label for="backup">Backup text</label></p>
  <textarea id="backup" rows="3"></textarea>
</div>

<script>
let words = ["sun", "moon"];

function render() {
  let list = document.getElementById("words");
  list.innerHTML = "";
  for (let word of words) {
    let li = document.createElement("li");
    li.textContent = word;
    list.appendChild(li);
  }
}

function addWord(event) {
  event.preventDefault();
  let word = document.getElementById("word").value.trim();
  if (word === "") {
    return;
  }
  words.push(word);
  document.getElementById("word").value = "";
  render();
}

render();

function backup() {
  // put words, as JSON text, into #backup
}
</script>
</body>
</html>

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 17: خروجی و نصب · چالش بعدی: Restore a Backup

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