简体   繁体   中英

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

I have two questions:

  1. How can I alert the user directly after they answer one question rather than alert them three times after they answer all three questions?
  2. Is there a way for me to keep track of how often the user answers correctly, and give the user a total score at the end of the quiz? don't need to give me exact code, just a little nudge for where I should be looking:)

See below for code:

<!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!");
}

Another option is to create a function that creates the numbers for you, so u don't have to copy-paste the prompts.

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();

Maybe try this? I haven't tested it:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM