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

جواب «Step 3: More Questions and a Score» چالش 3 از جلسه‌ی 20

سؤال

قدم سوم: چند سؤال و امتیاز. در بالای <script> (بیرون از تابع) این‌ها را بساز:
- آرایه‌ی questions با سه جمله: A cat says meow.، A spider has six legs.، The sun is a star.
- آرایه‌ی answers با جواب‌های درست به همان ترتیب: yes، no، yes
- let i = 0; (شماره‌ی سؤالِ الان) و let score = 0;
تابع answer را عوض کن:
- شرط بشود choice === answers[i]
- اگر جواب درست بود، یکی به score اضافه کن و آن را در تگِ score نشان بده.
- در آخرِ تابع (بعد از if و else) بنویس i = i + 1; و سؤالِ بعدی یعنی questions[i] را در تگِ question نشان بده.
بعد از سؤالِ سوم جای سؤال خالی می‌شود. نگران نباش، این را در قدم بعد درست می‌کنیم.

راهنما

جواب‌ها در آرایه متن هستند: let answers = ["yes", "no", "yes"];. خطِ آخرِ تابع: document.getElementById("question").textContent = questions[i];

یک جواب درست

<style>
body {
  text-align: center;
  font-family: Arial;
}
#question {
  font-size: 28px;
}
button {
  font-size: 20px;
  padding: 10px 30px;
}
</style>

<h1>Yes or No Quiz</h1>
<p id="question">A cat says meow.</p>
<div id="buttons">
  <button onclick="answer('yes')">Yes</button>
  <button onclick="answer('no')">No</button>
</div>
<p id="result"></p>
<p>Score: <span id="score">0</span></p>

<script>
let questions = ["A cat says meow.", "A spider has six legs.", "The sun is a star."];
let answers = ["yes", "no", "yes"];
let i = 0;
let score = 0;

function answer(choice) {
  if (choice === answers[i]) {
    score = score + 1;
    document.getElementById("score").textContent = score;
    document.getElementById("result").textContent = "Correct!";
  } else {
    document.getElementById("result").textContent = "Wrong!";
  }
  i = i + 1;
  document.getElementById("question").textContent = questions[i];
}
</script>

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