簡體   English   中英

如何在提示后直接提醒用戶而不是在他們回答所有三個提示后?

[英]How to alert user directly after prompt rather than after they answer all three prompts?

我有兩個問題:

  1. 如何在用戶回答一個問題后直接提醒他們,而不是在他們回答所有三個問題后提醒他們三次?
  2. 有沒有辦法讓我跟蹤用戶正確回答的頻率,並在測驗結束時給用戶一個總分? 不需要給我確切的代碼,只需輕輕推一下我應該看的地方:)

請參閱下面的代碼:

<!DOCTYPE html>

<html>

<p id="target"></p>

<button id="buttonclick" type="submit">Click me</button>

<script>

var questionOne = prompt("What is 2+2?", '');
var questionTwo = prompt("What is 1+1?", '');
var questionThree = prompt("What is 3+3?",'');

if (questionOne = 4) {
    alert("You got the question right!");
} else {
    alert("You got the question wrong!");
}

if (questionTwo = 2) {
    alert("You got the question right!");
} else {
    alert("You got the question wrong!");
}

if (questionThree = 6) {
    alert("You got the question right!");
} else {
    alert("You got the question wrong!");
}

</script>

</html>
if (prompt("What is 2+2?", '') == 4) {
    alert("You got the question right!");
} else {
    alert("You got the question wrong!");
}

if (prompt("What is 1+1?", '') == 2) {
    alert("You got the question right!");
} else {
    alert("You got the question wrong!");
}

if (prompt("What is 3+3?",'') == 6) {
    alert("You got the question right!");
} else {
    alert("You got the question wrong!");
}

另一種選擇是創建一個 function 來為您創建號碼,因此您不必復制粘貼提示。

const ask = () => {
    const n1 = Math.ceil(Math.random() * 100);
  const n2 = Math.ceil(Math.random() * 100);
  if (prompt(`What is ${n1}+${n2}?`, '') == n1 + n2) {
    alert("You got the question right!");
  } else {
      alert("You got the question wrong!");
  }
}

ask();
ask();
ask();

也許試試這個? 我沒有測試過:

<script>

var questionOne = prompt("What is 2+2?", '');

if (questionOne = 4) {
    alert("You got the question right!");
} else {
    alert("You got the question wrong!");
}

var questionTwo = prompt("What is 1+1?", '');

if (questionTwo = 2) {
    alert("You got the question right!");
} else {
    alert("You got the question wrong!");
}

var questionThree = prompt("What is 3+3?",'');

if (questionThree = 6) {
    alert("You got the question right!");
} else {
    alert("You got the question wrong!");
}

</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM