Why is my variable returning undefined in javascript?

ARI FISHER

I am new to javascript. I am making a flashcard program. For some reason, despite defining the variable lastPress as global, lastPress seems to return undefined. However, it is not resulting in an error in JSHint. Here's my code:

var i = 0;
var questions = ["1: Was the human migration caused by an ice age?", "True"];
var answer = [true, true];
var lastPress;
document.getElementById("question").innerHTML = questions[i];
function itsTrue() {
    lastPress = true;
}
function itsFalse() {
    lastPress = false;
}
function getAns() {
    if (answer[i] == lastPress) {
        document.getElementById("ans").innerHTML = "You're correct! The answer was " + lastPress;
    }
    else {
        document.getElementById("ans").innerHTML = "We're sorry, but that is not correct. You said " + lastPress + ". The answer was " + answer[i];
    }
    i++;
    document.getElementById("question").innerHTML = questions[i];
}
document.getElementById("true").onclick = function(){itsTrue()};
document.getElementById("true").onclick = function(){getAns()};
document.getElementById("false").onclick = function(){itsFalse()};
document.getElementById("false").onclick = function(){getAns()};
<!DOCTYPE HTML>
<html>
    <head>
        <title>Flash cards</title>
    </head>
    <body>
        <p id="question">This is where the question shows up</p>
        <p id="ans">This is where the result will show up</p>
        <button type="button" id="true">True</button>
        <button type="button" id="false">False</button>
    </body>
</html>

DCR

if you want to define multiple functions to the same click event: also, after the second press on your true false buttons i is incremented to 2 thus questions[2] is undefined

var i = 0;
            var questions = ["1: Was the human migration caused by an ice age?", "True"];
            var answer = [true, true];
            var lastPress;
            document.getElementById("question").innerHTML = questions[i];
            function itsTrue() {
                console.log('true')
                lastPress = true;                
            }
            function itsFalse() {
                console.log('false');
                lastPress = false;                
            }
            function getAns() {
                if (answer[i] == lastPress) {
                    document.getElementById("ans").innerHTML = "You're correct! The answer was " + lastPress;
                }
                else {
                    document.getElementById("ans").innerHTML = "We're sorry, but that is not correct. You said " + lastPress + ". The answer was " + answer[i];
                }
                i++;
                console.log(i);
                document.getElementById("question").innerHTML = questions[i];
            }
            document.getElementById("true").onclick = function(){itsTrue();getAns();};
            //document.getElementById("true").onclick = function(){getAns()};
            document.getElementById("false").onclick = function(){itsFalse(),getAns();};
           //  document.getElementById("false").onclick = function(){getAns()};
<!DOCTYPE HTML>
<html>
    <head>
        <title>Flash cards</title>
    </head>
    <body>
        <p id="question">This is where the question shows up</p>
        <p id="ans">This is where the result will show up</p>
        <button type="button" id="true">True</button>
        <button type="button" id="false">False</button>
    </body>
</html>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is my JavaScript hoisted local variable returning undefined but the hoisted global variable is returning blank?

Why is my variable tabledata returning undefined?

Why is my email check variable returning an error as undefined

Why is my variable returning as 'undefined' despite being set a value?

why my Nodejs / NestJs promise is returning undefined to variable in second function?

Why is my variable getting undefined in javascript?

why is my object returning undefined?

Why is my promise returning undefined?

Function returning undefined variable Javascript

Why is this javascript function returning undefined ?

Why is my intersectionObserver returning my querySelect as undefined?

my variable is returning empty why?

In JavaScript, why does this global variable become undefined in my function?

JavaScript - Why does my API call variable return undefined?

Why is my inner function returning as undefined in my simple Javascript OOP game

why does my functions returning "undefined"

Why is my recursive function returning undefined?

Why is my reducer returning undefined, React TypeScript

Why is my function returning an array with undefined as items?

why does my nodejs function returning undefined?

Why is my EJS variable returning "undefined" when i'm trying to use the mongoose "findByIdAndRemove" method

Why is the variable argument of this required function returning undefined?

Why is my imported variable undefined?

Why is Javascript returning "undefined" in Switch statement?

Why is my javascript parameter undefined?

Why is my tableView variable returning nil?

Why is my Swift string variable returning nil?

Why is my function returning 1 and not the variable value?

Why is my get request returning undefined in my image file tree?