annag153
annag153

Reputation: 13

simple javascript JS event handler not working correctly

Below is my code and the instructions that went with it. Currently the responseText for an incorrect answer displays on page load and I can't change it to the correct answer.

// Declare a string variable called question and set it equal to a True/False question of your choice. 
var question = "KD has sold his legacy at OKC for a championship ring at GS.";

// Declare a boolean variable called answer and set it equal to true or false (the answer to the question you asked above.) 
var answer = true;

// Create a function called loadQuestion() that sets the value of the questionText element equal to the question variable you declared above. 
function loadQuestion(){
document.getElementById("questionText").innerHTML = question;
}

// Create a function called checkAnswer(userAnswer) that takes one incoming parameter. 
function checkAnswer(userAnswer){
  if(userAnswer===answer){
    document.getElementById("responseText").innerHTML = "That is correct!";             
    document.getElementById("responseText").className = "correctAnswer";
  }
  else if(userAnswer!==answer){       
    document.getElementById("responseText").innerHTML = "I'm sorry, that is    
     not correct.";
    document.getElementById("responseText").className = "incorrectAnswer";
  }
}
// ---> If your answer variable matches the incoming userAnswer parameter, write a success message in the element called responseText. 
// ---> If your answer variable does NOT match the userAnswer parameter, write a failure message in the element called responseText. 

// Create TRADITIONAL DOM EVENT HANDLERS for the "onclick" events for the three buttons.
var start = document.getElementById("startButton");
var truth = document.getElementById("trueButton");
var falsify = document.getElementById("falseButton");
// The Start button should trigger the loadQuestion method
start.onclick = loadQuestion;
// The True button should trigger the checkAnswer method with a value of "true"
truth.onclick = checkAnswer("true");
// The False button should trigger the checkAnswer method with a value of "false"
falsify.onclick = checkAnswer("false");

Upvotes: 1

Views: 186

Answers (2)

Oleksandr
Oleksandr

Reputation: 5668

if you are comparing as triple equality === make sure that type is the same on both sides

Upvotes: 1

Alexey
Alexey

Reputation: 999

it should be

<...>.onclick = function() {
   checkAnswer("true");
}

instead of

<...>.onclick = checkAnswer("true");

Upvotes: 0

Related Questions