Cannot get if-statement to work within function











up vote
0
down vote

favorite












I'm a complete JS noob who set off on a mission to create a quiz as a first/second-ish project. I suppose there are better ways to do this than the way I'm doing it, but currently I, after a correct submission of the user, want to remove the current iteration from the array which is converted into text through innerHTML, and do the same for the correct answer/explanation. This works well, up until the last bit. After the user completes the last question, it again removes the data from the array, showing "UNDEFINED" in the screen. I figured that, by adding an if statement to see if array.length > 1, I could circumvent this. I tried avoiding this by having a different if statement return true or false and then using && on the function; to no avail. Any and all if statements here give me the errors:



Uncaught ReferenceError: nextQuestion is not defined
at submitAnswer (VM588 script.js:61)
at HTMLButtonElement.onclick (quizpage.html:19)



I've attached the html and JS underneath.






var questionsOverview = ["1 - 1+1 = ?", "2 - What food do dieters tend to avoid?", "3 - Best    fast food chain?"];
var answersOverview = ["[A] 1 [B] 3 [C] 2", "[A] Protein [B] Carbs [C] Glucose", "[A] Burger King [B] KFC [C] McDonalds"];
var answersLetter = ["C", "B", "C"];
var score = 0;
var answerUser = "test"

var currentQuestion = questionsOverview[0];

//set buttons as answersLetter
function setRed() {
document.getElementById(answerUser).style.background = "red";
}
function pressedA() {
answerUser = "A";
setRed();
document.getElementById("B").style.background = "";
document.getElementById("C").style.background = "";
};

function pressedB() {
answerUser = "B";
setRed();
document.getElementById("A").style.background = "";
document.getElementById("C").style.background = "";
};

function pressedC() {
answerUser = "C";
setRed();
document.getElementById("A").style.background = "";
document.getElementById("B").style.background = "";
};

//preps the first question/answer
function changeQuestion() {
document.getElementById("question").innerHTML = questionsOverview[0];
document.getElementById("answer").innerHTML = answersOverview[0];
};

//move to the next question
checkForEnd() && function nextQuestion() {
questionsOverview.shift();
answersOverview.shift();
answersLetter.shift();
changeQuestion();
};

// submit user answer
function submitAnswer() {
var audio = document.getElementById("tleeni");
audio.play();
if(answerUser === answersLetter[0]) {
alert("Correct! You're smart!");
nextQuestion();
}
else {
alert("Too bad!");
}
};

function checkForEnd() {
if (answersOverview.length > 1) {
return true;
} else {
return false;
}
};

  <head>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="reset.css">
<script src="script.js" type="text/javascript"></script>
</head>

<body onload="changeQuestion()">
<div class="top-header" id="header">
<font color="white">Quiz</font></div>
<div class="main-content" id="main-content">
<h2 id="question">1</h2>
<h3 id="answer">2</h3>
<h4 id="score>">scorefiller</h4>
<div id="buttons">
<button onClick="pressedA()" class="answers" id="A">A</button>
<button onClick="pressedB()" class="answers" id="B">B</button>
<button onClick="pressedC()" class="answers" id="C">C</button>
</div>
<button onClick="submitAnswer()" id="submit">Submit answer!</button>
</div>
<audio id="tleeni" src="submit.mp3"></audio>
</body>
<style>
@import url('https://fonts.googleapis.com/css?family=Open+Sans|Pacifico');
</style>





Given that I'm really new to programming I'd appreciate any and all feedback. Thank you very much in advance!










share|improve this question




























    up vote
    0
    down vote

    favorite












    I'm a complete JS noob who set off on a mission to create a quiz as a first/second-ish project. I suppose there are better ways to do this than the way I'm doing it, but currently I, after a correct submission of the user, want to remove the current iteration from the array which is converted into text through innerHTML, and do the same for the correct answer/explanation. This works well, up until the last bit. After the user completes the last question, it again removes the data from the array, showing "UNDEFINED" in the screen. I figured that, by adding an if statement to see if array.length > 1, I could circumvent this. I tried avoiding this by having a different if statement return true or false and then using && on the function; to no avail. Any and all if statements here give me the errors:



    Uncaught ReferenceError: nextQuestion is not defined
    at submitAnswer (VM588 script.js:61)
    at HTMLButtonElement.onclick (quizpage.html:19)



    I've attached the html and JS underneath.






    var questionsOverview = ["1 - 1+1 = ?", "2 - What food do dieters tend to avoid?", "3 - Best    fast food chain?"];
    var answersOverview = ["[A] 1 [B] 3 [C] 2", "[A] Protein [B] Carbs [C] Glucose", "[A] Burger King [B] KFC [C] McDonalds"];
    var answersLetter = ["C", "B", "C"];
    var score = 0;
    var answerUser = "test"

    var currentQuestion = questionsOverview[0];

    //set buttons as answersLetter
    function setRed() {
    document.getElementById(answerUser).style.background = "red";
    }
    function pressedA() {
    answerUser = "A";
    setRed();
    document.getElementById("B").style.background = "";
    document.getElementById("C").style.background = "";
    };

    function pressedB() {
    answerUser = "B";
    setRed();
    document.getElementById("A").style.background = "";
    document.getElementById("C").style.background = "";
    };

    function pressedC() {
    answerUser = "C";
    setRed();
    document.getElementById("A").style.background = "";
    document.getElementById("B").style.background = "";
    };

    //preps the first question/answer
    function changeQuestion() {
    document.getElementById("question").innerHTML = questionsOverview[0];
    document.getElementById("answer").innerHTML = answersOverview[0];
    };

    //move to the next question
    checkForEnd() && function nextQuestion() {
    questionsOverview.shift();
    answersOverview.shift();
    answersLetter.shift();
    changeQuestion();
    };

    // submit user answer
    function submitAnswer() {
    var audio = document.getElementById("tleeni");
    audio.play();
    if(answerUser === answersLetter[0]) {
    alert("Correct! You're smart!");
    nextQuestion();
    }
    else {
    alert("Too bad!");
    }
    };

    function checkForEnd() {
    if (answersOverview.length > 1) {
    return true;
    } else {
    return false;
    }
    };

      <head>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="reset.css">
    <script src="script.js" type="text/javascript"></script>
    </head>

    <body onload="changeQuestion()">
    <div class="top-header" id="header">
    <font color="white">Quiz</font></div>
    <div class="main-content" id="main-content">
    <h2 id="question">1</h2>
    <h3 id="answer">2</h3>
    <h4 id="score>">scorefiller</h4>
    <div id="buttons">
    <button onClick="pressedA()" class="answers" id="A">A</button>
    <button onClick="pressedB()" class="answers" id="B">B</button>
    <button onClick="pressedC()" class="answers" id="C">C</button>
    </div>
    <button onClick="submitAnswer()" id="submit">Submit answer!</button>
    </div>
    <audio id="tleeni" src="submit.mp3"></audio>
    </body>
    <style>
    @import url('https://fonts.googleapis.com/css?family=Open+Sans|Pacifico');
    </style>





    Given that I'm really new to programming I'd appreciate any and all feedback. Thank you very much in advance!










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm a complete JS noob who set off on a mission to create a quiz as a first/second-ish project. I suppose there are better ways to do this than the way I'm doing it, but currently I, after a correct submission of the user, want to remove the current iteration from the array which is converted into text through innerHTML, and do the same for the correct answer/explanation. This works well, up until the last bit. After the user completes the last question, it again removes the data from the array, showing "UNDEFINED" in the screen. I figured that, by adding an if statement to see if array.length > 1, I could circumvent this. I tried avoiding this by having a different if statement return true or false and then using && on the function; to no avail. Any and all if statements here give me the errors:



      Uncaught ReferenceError: nextQuestion is not defined
      at submitAnswer (VM588 script.js:61)
      at HTMLButtonElement.onclick (quizpage.html:19)



      I've attached the html and JS underneath.






      var questionsOverview = ["1 - 1+1 = ?", "2 - What food do dieters tend to avoid?", "3 - Best    fast food chain?"];
      var answersOverview = ["[A] 1 [B] 3 [C] 2", "[A] Protein [B] Carbs [C] Glucose", "[A] Burger King [B] KFC [C] McDonalds"];
      var answersLetter = ["C", "B", "C"];
      var score = 0;
      var answerUser = "test"

      var currentQuestion = questionsOverview[0];

      //set buttons as answersLetter
      function setRed() {
      document.getElementById(answerUser).style.background = "red";
      }
      function pressedA() {
      answerUser = "A";
      setRed();
      document.getElementById("B").style.background = "";
      document.getElementById("C").style.background = "";
      };

      function pressedB() {
      answerUser = "B";
      setRed();
      document.getElementById("A").style.background = "";
      document.getElementById("C").style.background = "";
      };

      function pressedC() {
      answerUser = "C";
      setRed();
      document.getElementById("A").style.background = "";
      document.getElementById("B").style.background = "";
      };

      //preps the first question/answer
      function changeQuestion() {
      document.getElementById("question").innerHTML = questionsOverview[0];
      document.getElementById("answer").innerHTML = answersOverview[0];
      };

      //move to the next question
      checkForEnd() && function nextQuestion() {
      questionsOverview.shift();
      answersOverview.shift();
      answersLetter.shift();
      changeQuestion();
      };

      // submit user answer
      function submitAnswer() {
      var audio = document.getElementById("tleeni");
      audio.play();
      if(answerUser === answersLetter[0]) {
      alert("Correct! You're smart!");
      nextQuestion();
      }
      else {
      alert("Too bad!");
      }
      };

      function checkForEnd() {
      if (answersOverview.length > 1) {
      return true;
      } else {
      return false;
      }
      };

        <head>
      <link rel="stylesheet" href="style.css">
      <link rel="stylesheet" href="reset.css">
      <script src="script.js" type="text/javascript"></script>
      </head>

      <body onload="changeQuestion()">
      <div class="top-header" id="header">
      <font color="white">Quiz</font></div>
      <div class="main-content" id="main-content">
      <h2 id="question">1</h2>
      <h3 id="answer">2</h3>
      <h4 id="score>">scorefiller</h4>
      <div id="buttons">
      <button onClick="pressedA()" class="answers" id="A">A</button>
      <button onClick="pressedB()" class="answers" id="B">B</button>
      <button onClick="pressedC()" class="answers" id="C">C</button>
      </div>
      <button onClick="submitAnswer()" id="submit">Submit answer!</button>
      </div>
      <audio id="tleeni" src="submit.mp3"></audio>
      </body>
      <style>
      @import url('https://fonts.googleapis.com/css?family=Open+Sans|Pacifico');
      </style>





      Given that I'm really new to programming I'd appreciate any and all feedback. Thank you very much in advance!










      share|improve this question















      I'm a complete JS noob who set off on a mission to create a quiz as a first/second-ish project. I suppose there are better ways to do this than the way I'm doing it, but currently I, after a correct submission of the user, want to remove the current iteration from the array which is converted into text through innerHTML, and do the same for the correct answer/explanation. This works well, up until the last bit. After the user completes the last question, it again removes the data from the array, showing "UNDEFINED" in the screen. I figured that, by adding an if statement to see if array.length > 1, I could circumvent this. I tried avoiding this by having a different if statement return true or false and then using && on the function; to no avail. Any and all if statements here give me the errors:



      Uncaught ReferenceError: nextQuestion is not defined
      at submitAnswer (VM588 script.js:61)
      at HTMLButtonElement.onclick (quizpage.html:19)



      I've attached the html and JS underneath.






      var questionsOverview = ["1 - 1+1 = ?", "2 - What food do dieters tend to avoid?", "3 - Best    fast food chain?"];
      var answersOverview = ["[A] 1 [B] 3 [C] 2", "[A] Protein [B] Carbs [C] Glucose", "[A] Burger King [B] KFC [C] McDonalds"];
      var answersLetter = ["C", "B", "C"];
      var score = 0;
      var answerUser = "test"

      var currentQuestion = questionsOverview[0];

      //set buttons as answersLetter
      function setRed() {
      document.getElementById(answerUser).style.background = "red";
      }
      function pressedA() {
      answerUser = "A";
      setRed();
      document.getElementById("B").style.background = "";
      document.getElementById("C").style.background = "";
      };

      function pressedB() {
      answerUser = "B";
      setRed();
      document.getElementById("A").style.background = "";
      document.getElementById("C").style.background = "";
      };

      function pressedC() {
      answerUser = "C";
      setRed();
      document.getElementById("A").style.background = "";
      document.getElementById("B").style.background = "";
      };

      //preps the first question/answer
      function changeQuestion() {
      document.getElementById("question").innerHTML = questionsOverview[0];
      document.getElementById("answer").innerHTML = answersOverview[0];
      };

      //move to the next question
      checkForEnd() && function nextQuestion() {
      questionsOverview.shift();
      answersOverview.shift();
      answersLetter.shift();
      changeQuestion();
      };

      // submit user answer
      function submitAnswer() {
      var audio = document.getElementById("tleeni");
      audio.play();
      if(answerUser === answersLetter[0]) {
      alert("Correct! You're smart!");
      nextQuestion();
      }
      else {
      alert("Too bad!");
      }
      };

      function checkForEnd() {
      if (answersOverview.length > 1) {
      return true;
      } else {
      return false;
      }
      };

        <head>
      <link rel="stylesheet" href="style.css">
      <link rel="stylesheet" href="reset.css">
      <script src="script.js" type="text/javascript"></script>
      </head>

      <body onload="changeQuestion()">
      <div class="top-header" id="header">
      <font color="white">Quiz</font></div>
      <div class="main-content" id="main-content">
      <h2 id="question">1</h2>
      <h3 id="answer">2</h3>
      <h4 id="score>">scorefiller</h4>
      <div id="buttons">
      <button onClick="pressedA()" class="answers" id="A">A</button>
      <button onClick="pressedB()" class="answers" id="B">B</button>
      <button onClick="pressedC()" class="answers" id="C">C</button>
      </div>
      <button onClick="submitAnswer()" id="submit">Submit answer!</button>
      </div>
      <audio id="tleeni" src="submit.mp3"></audio>
      </body>
      <style>
      @import url('https://fonts.googleapis.com/css?family=Open+Sans|Pacifico');
      </style>





      Given that I'm really new to programming I'd appreciate any and all feedback. Thank you very much in advance!






      var questionsOverview = ["1 - 1+1 = ?", "2 - What food do dieters tend to avoid?", "3 - Best    fast food chain?"];
      var answersOverview = ["[A] 1 [B] 3 [C] 2", "[A] Protein [B] Carbs [C] Glucose", "[A] Burger King [B] KFC [C] McDonalds"];
      var answersLetter = ["C", "B", "C"];
      var score = 0;
      var answerUser = "test"

      var currentQuestion = questionsOverview[0];

      //set buttons as answersLetter
      function setRed() {
      document.getElementById(answerUser).style.background = "red";
      }
      function pressedA() {
      answerUser = "A";
      setRed();
      document.getElementById("B").style.background = "";
      document.getElementById("C").style.background = "";
      };

      function pressedB() {
      answerUser = "B";
      setRed();
      document.getElementById("A").style.background = "";
      document.getElementById("C").style.background = "";
      };

      function pressedC() {
      answerUser = "C";
      setRed();
      document.getElementById("A").style.background = "";
      document.getElementById("B").style.background = "";
      };

      //preps the first question/answer
      function changeQuestion() {
      document.getElementById("question").innerHTML = questionsOverview[0];
      document.getElementById("answer").innerHTML = answersOverview[0];
      };

      //move to the next question
      checkForEnd() && function nextQuestion() {
      questionsOverview.shift();
      answersOverview.shift();
      answersLetter.shift();
      changeQuestion();
      };

      // submit user answer
      function submitAnswer() {
      var audio = document.getElementById("tleeni");
      audio.play();
      if(answerUser === answersLetter[0]) {
      alert("Correct! You're smart!");
      nextQuestion();
      }
      else {
      alert("Too bad!");
      }
      };

      function checkForEnd() {
      if (answersOverview.length > 1) {
      return true;
      } else {
      return false;
      }
      };

        <head>
      <link rel="stylesheet" href="style.css">
      <link rel="stylesheet" href="reset.css">
      <script src="script.js" type="text/javascript"></script>
      </head>

      <body onload="changeQuestion()">
      <div class="top-header" id="header">
      <font color="white">Quiz</font></div>
      <div class="main-content" id="main-content">
      <h2 id="question">1</h2>
      <h3 id="answer">2</h3>
      <h4 id="score>">scorefiller</h4>
      <div id="buttons">
      <button onClick="pressedA()" class="answers" id="A">A</button>
      <button onClick="pressedB()" class="answers" id="B">B</button>
      <button onClick="pressedC()" class="answers" id="C">C</button>
      </div>
      <button onClick="submitAnswer()" id="submit">Submit answer!</button>
      </div>
      <audio id="tleeni" src="submit.mp3"></audio>
      </body>
      <style>
      @import url('https://fonts.googleapis.com/css?family=Open+Sans|Pacifico');
      </style>





      var questionsOverview = ["1 - 1+1 = ?", "2 - What food do dieters tend to avoid?", "3 - Best    fast food chain?"];
      var answersOverview = ["[A] 1 [B] 3 [C] 2", "[A] Protein [B] Carbs [C] Glucose", "[A] Burger King [B] KFC [C] McDonalds"];
      var answersLetter = ["C", "B", "C"];
      var score = 0;
      var answerUser = "test"

      var currentQuestion = questionsOverview[0];

      //set buttons as answersLetter
      function setRed() {
      document.getElementById(answerUser).style.background = "red";
      }
      function pressedA() {
      answerUser = "A";
      setRed();
      document.getElementById("B").style.background = "";
      document.getElementById("C").style.background = "";
      };

      function pressedB() {
      answerUser = "B";
      setRed();
      document.getElementById("A").style.background = "";
      document.getElementById("C").style.background = "";
      };

      function pressedC() {
      answerUser = "C";
      setRed();
      document.getElementById("A").style.background = "";
      document.getElementById("B").style.background = "";
      };

      //preps the first question/answer
      function changeQuestion() {
      document.getElementById("question").innerHTML = questionsOverview[0];
      document.getElementById("answer").innerHTML = answersOverview[0];
      };

      //move to the next question
      checkForEnd() && function nextQuestion() {
      questionsOverview.shift();
      answersOverview.shift();
      answersLetter.shift();
      changeQuestion();
      };

      // submit user answer
      function submitAnswer() {
      var audio = document.getElementById("tleeni");
      audio.play();
      if(answerUser === answersLetter[0]) {
      alert("Correct! You're smart!");
      nextQuestion();
      }
      else {
      alert("Too bad!");
      }
      };

      function checkForEnd() {
      if (answersOverview.length > 1) {
      return true;
      } else {
      return false;
      }
      };

        <head>
      <link rel="stylesheet" href="style.css">
      <link rel="stylesheet" href="reset.css">
      <script src="script.js" type="text/javascript"></script>
      </head>

      <body onload="changeQuestion()">
      <div class="top-header" id="header">
      <font color="white">Quiz</font></div>
      <div class="main-content" id="main-content">
      <h2 id="question">1</h2>
      <h3 id="answer">2</h3>
      <h4 id="score>">scorefiller</h4>
      <div id="buttons">
      <button onClick="pressedA()" class="answers" id="A">A</button>
      <button onClick="pressedB()" class="answers" id="B">B</button>
      <button onClick="pressedC()" class="answers" id="C">C</button>
      </div>
      <button onClick="submitAnswer()" id="submit">Submit answer!</button>
      </div>
      <audio id="tleeni" src="submit.mp3"></audio>
      </body>
      <style>
      @import url('https://fonts.googleapis.com/css?family=Open+Sans|Pacifico');
      </style>






      javascript html if-statement






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 23:30









      MuratK

      1254




      1254










      asked Nov 11 at 22:43









      Yoram

      11




      11
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote













          I think your issue might be here



          //move to the next question
          checkForEnd() && function nextQuestion() {
          questionsOverview.shift();
          answersOverview.shift();
          answersLetter.shift();
          changeQuestion();
          };


          You're not actually calling the function nextQuestion; you're only making a boolean check that is equivalent to checkForEnd() && true since a function is a truthy value.



          The function declaration is also "lost" hence why you're getting that reference error.



          true && function burrito() {
          return '🌯';
          }
          burrito();
          // VM1158:1 Uncaught ReferenceError: burrito is not defined





          share|improve this answer




























            up vote
            0
            down vote













            checkForEnd() && function nextQuestion() {}


            This doesn't run each time you click the button, nor does it run each time nextQuestion() is called. It does it once, whenever the code is first loaded. Even if it did all it would be doing is creating a named function expression, which is not useable outside the function itself, ie you can't call nextQuestion().



            What you want is to use checkForEnd() inside the function and return based on that



            function nextQuestion() {
            if(!checkForEnd()) return;
            questionsOverview.shift();
            answersOverview.shift();
            answersLetter.shift();
            changeQuestion();
            };





            share|improve this answer























            • nextQuestion is never defined either (even if checkForEnd() evaluates to true), @customcommander is right here
              – shkaper
              Nov 11 at 23:02










            • @shkaper correct forgot that was a named function expression which doesn't define a function into the scope. Though the solution I provided would still fix OP's problem.
              – Patrick Evans
              Nov 11 at 23:10











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253989%2fcannot-get-if-statement-to-work-within-function%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote













            I think your issue might be here



            //move to the next question
            checkForEnd() && function nextQuestion() {
            questionsOverview.shift();
            answersOverview.shift();
            answersLetter.shift();
            changeQuestion();
            };


            You're not actually calling the function nextQuestion; you're only making a boolean check that is equivalent to checkForEnd() && true since a function is a truthy value.



            The function declaration is also "lost" hence why you're getting that reference error.



            true && function burrito() {
            return '🌯';
            }
            burrito();
            // VM1158:1 Uncaught ReferenceError: burrito is not defined





            share|improve this answer

























              up vote
              2
              down vote













              I think your issue might be here



              //move to the next question
              checkForEnd() && function nextQuestion() {
              questionsOverview.shift();
              answersOverview.shift();
              answersLetter.shift();
              changeQuestion();
              };


              You're not actually calling the function nextQuestion; you're only making a boolean check that is equivalent to checkForEnd() && true since a function is a truthy value.



              The function declaration is also "lost" hence why you're getting that reference error.



              true && function burrito() {
              return '🌯';
              }
              burrito();
              // VM1158:1 Uncaught ReferenceError: burrito is not defined





              share|improve this answer























                up vote
                2
                down vote










                up vote
                2
                down vote









                I think your issue might be here



                //move to the next question
                checkForEnd() && function nextQuestion() {
                questionsOverview.shift();
                answersOverview.shift();
                answersLetter.shift();
                changeQuestion();
                };


                You're not actually calling the function nextQuestion; you're only making a boolean check that is equivalent to checkForEnd() && true since a function is a truthy value.



                The function declaration is also "lost" hence why you're getting that reference error.



                true && function burrito() {
                return '🌯';
                }
                burrito();
                // VM1158:1 Uncaught ReferenceError: burrito is not defined





                share|improve this answer












                I think your issue might be here



                //move to the next question
                checkForEnd() && function nextQuestion() {
                questionsOverview.shift();
                answersOverview.shift();
                answersLetter.shift();
                changeQuestion();
                };


                You're not actually calling the function nextQuestion; you're only making a boolean check that is equivalent to checkForEnd() && true since a function is a truthy value.



                The function declaration is also "lost" hence why you're getting that reference error.



                true && function burrito() {
                return '🌯';
                }
                burrito();
                // VM1158:1 Uncaught ReferenceError: burrito is not defined






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 22:58









                customcommander

                1,004616




                1,004616
























                    up vote
                    0
                    down vote













                    checkForEnd() && function nextQuestion() {}


                    This doesn't run each time you click the button, nor does it run each time nextQuestion() is called. It does it once, whenever the code is first loaded. Even if it did all it would be doing is creating a named function expression, which is not useable outside the function itself, ie you can't call nextQuestion().



                    What you want is to use checkForEnd() inside the function and return based on that



                    function nextQuestion() {
                    if(!checkForEnd()) return;
                    questionsOverview.shift();
                    answersOverview.shift();
                    answersLetter.shift();
                    changeQuestion();
                    };





                    share|improve this answer























                    • nextQuestion is never defined either (even if checkForEnd() evaluates to true), @customcommander is right here
                      – shkaper
                      Nov 11 at 23:02










                    • @shkaper correct forgot that was a named function expression which doesn't define a function into the scope. Though the solution I provided would still fix OP's problem.
                      – Patrick Evans
                      Nov 11 at 23:10















                    up vote
                    0
                    down vote













                    checkForEnd() && function nextQuestion() {}


                    This doesn't run each time you click the button, nor does it run each time nextQuestion() is called. It does it once, whenever the code is first loaded. Even if it did all it would be doing is creating a named function expression, which is not useable outside the function itself, ie you can't call nextQuestion().



                    What you want is to use checkForEnd() inside the function and return based on that



                    function nextQuestion() {
                    if(!checkForEnd()) return;
                    questionsOverview.shift();
                    answersOverview.shift();
                    answersLetter.shift();
                    changeQuestion();
                    };





                    share|improve this answer























                    • nextQuestion is never defined either (even if checkForEnd() evaluates to true), @customcommander is right here
                      – shkaper
                      Nov 11 at 23:02










                    • @shkaper correct forgot that was a named function expression which doesn't define a function into the scope. Though the solution I provided would still fix OP's problem.
                      – Patrick Evans
                      Nov 11 at 23:10













                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    checkForEnd() && function nextQuestion() {}


                    This doesn't run each time you click the button, nor does it run each time nextQuestion() is called. It does it once, whenever the code is first loaded. Even if it did all it would be doing is creating a named function expression, which is not useable outside the function itself, ie you can't call nextQuestion().



                    What you want is to use checkForEnd() inside the function and return based on that



                    function nextQuestion() {
                    if(!checkForEnd()) return;
                    questionsOverview.shift();
                    answersOverview.shift();
                    answersLetter.shift();
                    changeQuestion();
                    };





                    share|improve this answer














                    checkForEnd() && function nextQuestion() {}


                    This doesn't run each time you click the button, nor does it run each time nextQuestion() is called. It does it once, whenever the code is first loaded. Even if it did all it would be doing is creating a named function expression, which is not useable outside the function itself, ie you can't call nextQuestion().



                    What you want is to use checkForEnd() inside the function and return based on that



                    function nextQuestion() {
                    if(!checkForEnd()) return;
                    questionsOverview.shift();
                    answersOverview.shift();
                    answersLetter.shift();
                    changeQuestion();
                    };






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 11 at 23:08

























                    answered Nov 11 at 22:55









                    Patrick Evans

                    31.7k54470




                    31.7k54470












                    • nextQuestion is never defined either (even if checkForEnd() evaluates to true), @customcommander is right here
                      – shkaper
                      Nov 11 at 23:02










                    • @shkaper correct forgot that was a named function expression which doesn't define a function into the scope. Though the solution I provided would still fix OP's problem.
                      – Patrick Evans
                      Nov 11 at 23:10


















                    • nextQuestion is never defined either (even if checkForEnd() evaluates to true), @customcommander is right here
                      – shkaper
                      Nov 11 at 23:02










                    • @shkaper correct forgot that was a named function expression which doesn't define a function into the scope. Though the solution I provided would still fix OP's problem.
                      – Patrick Evans
                      Nov 11 at 23:10
















                    nextQuestion is never defined either (even if checkForEnd() evaluates to true), @customcommander is right here
                    – shkaper
                    Nov 11 at 23:02




                    nextQuestion is never defined either (even if checkForEnd() evaluates to true), @customcommander is right here
                    – shkaper
                    Nov 11 at 23:02












                    @shkaper correct forgot that was a named function expression which doesn't define a function into the scope. Though the solution I provided would still fix OP's problem.
                    – Patrick Evans
                    Nov 11 at 23:10




                    @shkaper correct forgot that was a named function expression which doesn't define a function into the scope. Though the solution I provided would still fix OP's problem.
                    – Patrick Evans
                    Nov 11 at 23:10


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253989%2fcannot-get-if-statement-to-work-within-function%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Bressuire

                    Vorschmack

                    Quarantine