1

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");
5
  • If you know it should be start.onclick = loadQuestion;, then why did you try like checkAnswer("true"); ? Commented Jul 7, 2016 at 16:04
  • Why didn't you write loadQuestion() instead of loadQuestion ? Commented Jul 7, 2016 at 16:08
  • 1
    do you mean i'm comparing a string w/ a boolean? i corrected that and its working now, thanks! Commented Jul 7, 2016 at 16:10
  • oh.. when you write it as a simple e.onclick = function, you don't need the parentheses, unless you have parameters, and then you have to use the anonymous function, which i was doing wrong Commented Jul 7, 2016 at 16:12
  • Yes, The thing is, onclick or any event expects handler to be an function expression.. You were passing undefined as function was being invoked and it returned nothing(undefined) Commented Jul 7, 2016 at 16:23

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

it should be

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

instead of

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

1 Comment

this worked great.. i also had to make sure i had the answer var as a string. thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.