0

I keep getting this error due to these two lines:

    document.getElementById('button').innerHTML = '<p><button 
    onClick = "MultiAnswer('+ questions[output] + ',' + answer[output] 
    +');">Submit</button></p>';

And I can't figure out what I am missing .

Edit: Here is the surrounding code (Excuse the mess) Contains methods that uses a switch statement to determine the input for the arrays required, from there puts it into the parameters for DisplayQuestion which then passes it to the functions below from the behaviour wanted:

function MultiQuest(questions, choices, answer){
    var output = Math.floor(Math.random() * (questions.length));
    var choicesOut = [];

    document.getElementById('question').innerHTML = '<p id = "Q1">' + questions[output] + '<p><br>';

    for(var k = 0;k < choices[output].length; k++ ){
        choicesOut.push('<p><input id = "choice'+[k]+'" type = "radio" name = "option" value="'+choices[output][k]+'">' + choices[output][k] + '<p>');    
    }
    document.getElementById('answers').innerHTML = choicesOut.join("");
    document.getElementById('button').innerHTML = '<p><button onClick = "MultiAnswer('+ questions[output] + ',' + answer[output] +');">Submit</button></p>';
    document.getElementById('score').innerHTML = '<p>' + score + '<p>';
}

function MultiAnswer(questions, answer, pageType){

     var currentQuestion = document.getElementById('Q1').textContent;
     var number = multiQuestions(currentQuestion, questions);
     var correctAnswer = answer[number];
     var givenAnswer;

     var options = document.getElementsByName('option');
     var i
     for(i = 0; i < options.length; i++){
        if(options[i].checked){
            givenAnswer = options[i].value;
        }
     }

    if(givenAnswer == correctAnswer){
        alert("Right Answer!");
        score++;
    } else {
        alert("Wrong Answer!");
        score = 0;
    }
    i = 0;
    DisplayQuestion(pageType);
}

 function multiQuestions(currentQuestion, whichArray){
    for(var i = 0; i < multiquestions.length; i++){
        if(currentQuestion == whichArray[i]){
           return i;
        }
   }
   return null;
 }
  • Please paste your code and mark the lines that are causing the issue. – fadeys.work Feb 22 '17 at 18:54
  • It's likely not the code you've pasted here, because I can copy and paste just that without getting the same error. Show more of the surrounding code - it's likely a problem in the line or two leading up to this one. – Jason Feb 22 '17 at 19:09
  • Do you have the code broken in different lines ? Since you are building a string, having it in different lines could cause the issue. Because the js lines work fine https://jsfiddle.net/cmhb465u/15/ – Imprfectluck Feb 22 '17 at 19:10
  • What are the values of questions[output] and answer[output] ? – manassehkatz-Moving 2 Codidact Feb 22 '17 at 19:10

2 Answers2

1

You cannot have a function call like this:

MultiAnswer('+ questions[output] + ',' + answer[output] 
+')

You will need to evaluate the parameter in a seperate variable and then pass it in the function.

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
0

So in your onClick call of multiAnswer you have wrapped the 3 inputs in quotes. After referencing your multiAnswer function you do have the 3 inputs that you are looking for. You also have + signs on the ends of those inputs. You do not need to concatenate the parens inside of the function call.

I hope this helps! https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions

onClick = "MultiAnswer(questions[output] + ',' + answer[output] 
  )">Submit</button></p>';