0

I am building a cute webpage for my teacher (who's very fun) as a Christmas present, but whenever I enter text into the input box and submit any question past the first, the code marks it as incorrect. I thought I had fixed this problem when I changed the question submit, every time the question changes, but apparently not. Can someone tell me what I'm doing wrong?

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Funn Chunn | Quiz</title>
    <link rel="icon" href="images/alabama logo.png"/>
    <link href="style.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
  </head>
  <body onload="firstQ();">
    <div class='container'>
      <div class='row'>
        <h1 id='mainHeader'>Funn Chunn | Quiz</h1>
      </div>

      <div class='row'>
        <h3 id='questionBox'></h3>
      </div>

      <div class='row'>
        <form id="inputBoxOuter">
          <input id='inputBox' type='text'></br>
          <button class='btn btn-lg btn-info' id='submitBtn' onclick='firstQAnswer();'>Enter</button>
        </form>
      </div>

      <div class='row'>
        <form id="playAgainOuter">

        </form>
      </div>

      <div class="row">
        <p id="subText"></p>
      </div>

      <div class="row">
        <div id="logoImgOuter"><img class="mx-auto" id="logoImg" src="images/alabama logo.png"/></div>
      </div>
    </div>

    <script src="script.js"></script>
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
  </body>
</html>

JavaScript

var mainHeaderJs = document.getElementById("mainHeader");
var questionBoxJs = document.getElementById("questionBox");
var inputBoxJs = document.getElementById("inputBox");
var inputBoxOuterJs = document.getElementById("inputBoxOuter");
var subTextJs = document.getElementById("subText");
var logoImgOuterJs = document.getElementById("logoImgOuter");
var playAgainOuter = document.getElementById("playAgainOuter");

var playerScore = 0;

function finalScore() {
  mainHeaderJs.innerHTML = "You Got a Score of " + playerScore + " Out of 10";
  playAgainOuter.innerHTML = "<button class='btn btn-lg btn-info mx-auto' id='playAgain' onclick='playAgain();'>Play Again</button>"
  questionBoxJs.innerHTML = "";
  inputBoxOuterJs.innerHTML = "";
}

function playAgain() {
  mainHeaderJs.innerHTML = "Funn Chunn | Quiz";
  firstQ();
  playerScore = 0;
  playAgainOuter.innerHTML = "";
  inputBoxOuterJs.innerHTML = "<input id='inputBox' type='text'></br><button class='btn btn-info' id='submitBtn' onclick='firstQAnswer();'>Enter</button>";
}

function moveOnGood() {
  subTextJs.innerHTML = "Correct! Moving on to the next question";
  setTimeout(function () {
    subTextJs.innerHTML = "";
  }, 2000);
}

function moveOnBad() {
  subTextJs.innerHTML = "Moving on to the next question...";
  setTimeout(function () {
    subTextJs.innerHTML = "";
  }, 2000);
}

function firstQ() {
  setTimeout(function () {
    questionBoxJs.innerHTML = "Question 1: What is Mrs. Chunn's Favorite Team?";
  }, 2000);
}

function firstQAnswer() {
var Q1Answer = inputBoxJs.value;
Q1Answer = Q1Answer.toUpperCase();
  if (Q1Answer == "ALABAMA") {
    inputBoxOuterJs.innerHTML = "<input id='inputBox' type='text'></br><button class='btn btn-info' id='submitBtn' onclick='secondQAnswer();'>Enter</button>";
    playerScore += 1;
    moveOnGood();
    secondQ();
  } else if (Q1Answer == "CRIMSON") {
    inputBoxOuterJs.innerHTML = "<input id='inputBox' type='text'></br><button class='btn btn-info' id='submitBtn' onclick='secondQAnswer();'>Enter</button>";
    playerScore += 1;
    moveOnGood();
    secondQ();
  } else if (Q1Answer == "CRIMSON TIDE") {
    inputBoxOuterJs.innerHTML = "<input id='inputBox' type='text'></br><button class='btn btn-info' id='submitBtn' onclick='secondQAnswer();'>Enter</button>";
    playerScore += 1;
    moveOnGood();
    secondQ();
  } else {
    inputBoxOuterJs.innerHTML = "<input id='inputBox' type='text'></br><button class='btn btn-info' id='submitBtn' onclick='secondQAnswer();'>Enter</button>";
    moveOnBad();
    secondQ();
  }
}

function secondQ() {
  setTimeout(function () {
    questionBoxJs.innerHTML = "Question 2: Is Mrs. Chunn a Millenial?";
  }, 2000);
}

function secondQAnswer() {
var Q2Answer = inputBoxJs.value;
Q2Answer = Q2Answer.toUpperCase();
  if (Q2Answer == "YES") {
    inputBoxOuterJs.innerHTML = "<input id='inputBox' type='text'></br><button class='btn btn-info' id='submitBtn' onclick='thirdQAnswer();'>Enter</button>";
    playerScore += 1;
    moveOnGood();
    thirdQ();
  } else if (Q2Answer == "YEAH") {
    inputBoxOuterJs.innerHTML = "<input id='inputBox' type='text'></br><button class='btn btn-info' id='submitBtn' onclick='thirdQAnswer();'>Enter</button>";
    playerScore += 1;
    moveOnGood();
    thirdQ();
  } else if (Q2Answer == "Y") {
    inputBoxOuterJs.innerHTML = "<input id='inputBox' type='text'></br><button class='btn btn-info' id='submitBtn' onclick='thirdQAnswer();'>Enter</button>";
    playerScore += 1;
    moveOnGood();
    thirdQ();
  } else {
    inputBoxOuterJs.innerHTML = "<input id='inputBox' type='text'></br><button class='btn btn-info' id='submitBtn' onclick='secondQAnswerAlt();'>Enter</button>";
    moveOnBad();
    thirdQ();
  }
}
booliam
  • 33
  • 4

1 Answers1

1

This isn't the best solution, but its one that should work. Just initialize inputBoxjs = ''; and update inputBoxjs at the start of every question function.

So change

var inputBoxJs = document.getElementById("inputBox");

to

var inputBoxJs = '';

and add this line

inputBoxJs = document.getElementById("inputBox");

above your Q1Answer and Q2Answer variables.

Then just add your thirdQ function and variables after you're finished with your Q2 functions.

Hopefully this helps.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Funn Chunn | Quiz</title>
    <link rel="icon" href="images/alabama logo.png"/>
    <link href="style.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
  </head>
  <body onload="firstQ();">
    <div class='container'>
      <div class='row'>
        <h1 id='mainHeader'>Funn Chunn | Quiz</h1>
      </div>

      <div class='row'>
        <h3 id='questionBox'></h3>
      </div>

      <div class='row'>
        <form id="inputBoxOuter">
          <input id='inputBox' type='text'> <br/>
          <button class='btn btn-lg btn-info' id='submitBtn' onclick='firstQAnswer();'>Enter</button>
        </form>
      </div>

      <div class='row'>
        <form id="playAgainOuter">

        </form>
      </div>

      <div class="row">
        <p id="subText"></p>
      </div>

      <div class="row">
        <div id="logoImgOuter"><img class="mx-auto" id="logoImg" src="images/alabama logo.png"/></div>
      </div>
    </div>

    <script src="script.js"></script>
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
  </body>
</html>
<script>
var mainHeaderJs = document.getElementById("mainHeader");
var questionBoxJs = document.getElementById("questionBox");
var inputBoxJs = '';
var inputBoxOuterJs = document.getElementById("inputBoxOuter");
var subTextJs = document.getElementById("subText");
var logoImgOuterJs = document.getElementById("logoImgOuter");
var playAgainOuter = document.getElementById("playAgainOuter");

var playerScore = 0;

function finalScore() {
  mainHeaderJs.innerHTML = "You Got a Score of " + playerScore + " Out of 10";
  playAgainOuter.innerHTML = "<button class='btn btn-lg btn-info mx-auto' id='playAgain' onclick='playAgain();'>Play Again</button>"
  questionBoxJs.innerHTML = "";
  inputBoxOuterJs.innerHTML = "";
}

function playAgain() {
  mainHeaderJs.innerHTML = "Funn Chunn | Quiz";
  firstQ();
  playerScore = 0;
  playAgainOuter.innerHTML = "";
  inputBoxOuterJs.innerHTML = "<input id='inputBox' type='text'></br><button class='btn btn-info' id='submitBtn' onclick='firstQAnswer();'>Enter</button>";
}

function moveOnGood() {
  subTextJs.innerHTML = "Correct! Moving on to the next question";
  setTimeout(function () {
    subTextJs.innerHTML = "";
  }, 2000);
}

function moveOnBad() {
  subTextJs.innerHTML = "Moving on to the next question...";
  setTimeout(function () {
    subTextJs.innerHTML = "";
  }, 2000);
}

function firstQ() {
  setTimeout(function () {
    questionBoxJs.innerHTML = "Question 1: What is Mrs. Chunn's Favorite Team?";
  }, 2000);
}

function firstQAnswer() {
inputBoxJs = document.getElementById("inputBox");
var Q1Answer = inputBoxJs.value;
Q1Answer = Q1Answer.toUpperCase();
  if (Q1Answer == "ALABAMA") {
    inputBoxOuterJs.innerHTML = "<input id='inputBox' type='text'></br><button class='btn btn-info' id='submitBtn' onclick='secondQAnswer();'>Enter</button>";
    playerScore += 1;
    moveOnGood();
    secondQ();
  } else if (Q1Answer == "CRIMSON") {
    inputBoxOuterJs.innerHTML = "<input id='inputBox' type='text'></br><button class='btn btn-info' id='submitBtn' onclick='secondQAnswer();'>Enter</button>";
    playerScore += 1;
    moveOnGood();
    secondQ();
  } else if (Q1Answer == "CRIMSON TIDE") {
    inputBoxOuterJs.innerHTML = "<input id='inputBox' type='text'></br><button class='btn btn-info' id='submitBtn' onclick='secondQAnswer();'>Enter</button>";
    playerScore += 1;
    moveOnGood();
    secondQ();
  } else {
    inputBoxOuterJs.innerHTML = "<input id='inputBox' type='text'></br><button class='btn btn-info' id='submitBtn' onclick='secondQAnswer();'>Enter</button>";
    moveOnBad();
    secondQ();
  }
}

function secondQ() {
  setTimeout(function () {
    questionBoxJs.innerHTML = "Question 2: Is Mrs. Chunn a Millenial?";
  }, 2000);
}

function secondQAnswer() {
inputBoxJs = document.getElementById("inputBox");
var Q2Answer = inputBoxJs.value;
Q2Answer = Q2Answer.toUpperCase();
  if (Q2Answer == "YES") {
    inputBoxOuterJs.innerHTML = "<input id='inputBox' type='text'></br><button class='btn btn-info' id='submitBtn' onclick='thirdQAnswer();'>Enter</button>";
    playerScore += 1;
    moveOnGood();
    thirdQ();
  } else if (Q2Answer == "YEAH") {
    inputBoxOuterJs.innerHTML = "<input id='inputBox' type='text'></br><button class='btn btn-info' id='submitBtn' onclick='thirdQAnswer();'>Enter</button>";
    playerScore += 1;
    moveOnGood();
    thirdQ();
  } else if (Q2Answer == "Y") {
    inputBoxOuterJs.innerHTML = "<input id='inputBox' type='text'></br><button class='btn btn-info' id='submitBtn' onclick='thirdQAnswer();'>Enter</button>";
    playerScore += 1;
    moveOnGood();
    thirdQ();
  } else {
    inputBoxOuterJs.innerHTML = "<input id='inputBox' type='text'></br><button class='btn btn-info' id='submitBtn' onclick='secondQAnswerAlt();'>Enter</button>";
    moveOnBad();
    thirdQ();
  }
}
</script>
King11
  • 1,239
  • 3
  • 9
  • 17
  • I tried the method that King11 provided, and it worked great, but then I added onto it and it stopped working. Check the code out at: https://funn-chunn.booliam.repl.co/script.js – booliam Dec 19 '19 at 18:37
  • @booliam You added code that only corresponds to the click of the first question, not the other ones... `showInput()` in `secondQ` and `thirdQ` functions will never go to the `secondQAnswer` or `thirdQAnswer`. – King11 Dec 19 '19 at 18:56
  • @booliam Also why unmark this as the accepted answer? The answer I posted initially resolved your original issue. After that you added code and now your code does not work as you intended it to. The new issue that you created with added code should be a new question, since your initial and original question was resolved at first. – King11 Dec 19 '19 at 19:01
  • Sorry, it was a mistake. I apologize – booliam Dec 19 '19 at 20:32