Question

I'm new to javascript and I'm trying to create a dynamic list of quiz questions in business catalyst. With the way BC is set up you can use {tags} to place user-created information on a page.

That said I'm trying to generate a list of quiz questions based on user selected values. I have created an object, "Question", and placed the necessary properties with their values into the newly defined objects below.

In my code I am currently trying to:

1 - Define the Question object class

2 - Define the 15 possible quiz questions

3 - Write a for loop that will write each question based on a key value from the previous question.

The funciton/for loop I've written will ultimately do this:

Write question 1

if question1 key has a value of 'Yes' then write question 2

if question2 has a key value of 'Yes' then write question 3

etc etc until there are either no more yes's or question 15 has been written.

When I try to execute my code below I get the error 'Uncaught ReferenceError: question is not defined' Can someone help me understand what I'm doing wrong? Thank you for any and all help!

NOTE: TO SAVE SPACE I HAVE ONLY INCLUDED THREE QUESTION VARIABLES. IN MY ACTUAL CODE I HAVE DEFINED 15 QUESTION OBJECTS.

<script>
    function Question (questionNumber, questionText, answerType, answerA, answerB, answerC, answerD, correctAnswer, visualRef, refKey, nextQ, qTextField, aTypeField, mcAField, mcBField, mcCField, mcDField, mcUserAnswer, tfUserAnswer, sRatings, sSAnswer, passFail) {
    this.questionNumber = questionNumber;
    this.questionText = questionText;
    this.answerType = answerType;
    this.answerA = answerA;
    this.answerB = answerB;
    this.answerC = answerC;
    this.answerD = answerD;
    this.true = "True";
    this.false = "False";
    this.correctAnswer = correctAnswer;
    this.visualRef = visualRef;
    this.refKey = refKey;
    this.nextQ = nextQ;
    this.qTextField = qTextField;
    this.aTypeField = aTypeField;
    this.mcAField = mcAField;
    this.mcBField = mcBField;
    this.mcCField = mcCField;
    this.mcDField = mcDField;
    this.mcUserAnswer = mcUserAnswer;
    this.tfUserAnswer = tfUserAnswer;
    this.sRatings = sRatings;
    this.sSAnswer = sSAnswer;
    this.passFail = passFail;
    this.createQuestion = function() {
            document.write("This is writing question " + this.questionNumber );
        };
    };

    var question1 = new Question("1", "{tag_q-question_1}", "{tag_q-answer-type_1}", "{tag_q-text-answer_101}", "{tag_q-text-answer_102}", "{tag_q-text-answer_103}", "{tag_q-text-answer_104}", "{tag_q-multichoice-answer_1}{tag_q-t/f-answer_1}", "{tag_q-visual-reference_1}", "{tag_q-youtube_1}{tag_q-vimeo_1}{tag_q-image_1_value}", "{tag_q-next-question_1}", "CAT_Custom_13", "CAT_Custom_11", "CAT_Custom_14", "CAT_Custom_15", "CAT_Custom_16", "CAT_Custom_17", "CAT_Custom_7", "CAT_Custom_8", "CAT_Custom_9", "CAT_Custom_10", "CAT_Custom_12");
    var question2 = new Question("2", "{tag_q-question_2}", "{tag_q-answer-type_2}", "{tag_q-text-answer_201}", "{tag_q-text-answer_202}", "{tag_q-text-answer_203}", "{tag_q-text-answer_204}", "{tag_q-multichoice-answer_2}{tag_q-t/f-answer_2}", "{tag_q-visual-reference_2}", "{tag_q-youtube_2}{tag_q-vimeo_2}{tag_q-image_2_value}", "{tag_q-next-question_2}", "CAT_Custom_19", "CAT_Custom_20", "CAT_Custom_22", "CAT_Custom_23", "CAT_Custom_24", "CAT_Custom_25", "CAT_Custom_21", "CAT_Custom_26", "CAT_Custom_27", "CAT_Custom_28", "CAT_Custom_29");
    var question3 = new Question("3", "{tag_q-question_3}", "{tag_q-answer-type_3}", "{tag_q-text-answer_301}", "{tag_q-text-answer_302}", "{tag_q-text-answer_303}", "{tag_q-text-answer_304}", "{tag_q-multichoice-answer_3}{tag_q-t/f-answer_3}", "{tag_q-visual-reference_3}", "{tag_q-youtube_3}{tag_q-vimeo_3}{tag_q-image_3_value}", "{tag_q-next-question_3}", "CAT_Custom_30", "CAT_Custom_31", "CAT_Custom_33", "CAT_Custom_34", "CAT_Custom_35", "CAT_Custom_36", "CAT_Custom_32", "CAT_Custom_37", "CAT_Custom_38", "CAT_Custom_39", "CAT_Custom_40");

    for (var i = 1; i <= 15; i++) {
        if (question[i].prototype.nextQ === "Yes") {
            question[i].createQuestion();
        } else {
            question1.createQuestion();
        };
    }   

</script>
Was it helpful?

Solution

You have created 3 individual objects and you are trying to access them as an array however they are individuals not an array.

Try this

var question = new Array();

question[1] = new Question("1", "{tag_q-question_1}", "{tag_q-answer-type_1}", "{tag_q-text-answer_101}", "{tag_q-text-answer_102}", "{tag_q-text-answer_103}", "{tag_q-text-answer_104}", "{tag_q-multichoice-answer_1}{tag_q-t/f-answer_1}", "{tag_q-visual-reference_1}", "{tag_q-youtube_1}{tag_q-vimeo_1}{tag_q-image_1_value}", "{tag_q-next-question_1}", "CAT_Custom_13", "CAT_Custom_11", "CAT_Custom_14", "CAT_Custom_15", "CAT_Custom_16", "CAT_Custom_17", "CAT_Custom_7", "CAT_Custom_8", "CAT_Custom_9", "CAT_Custom_10", "CAT_Custom_12");
question[2] = new Question("2", "{tag_q-question_2}", "{tag_q-answer-type_2}", "{tag_q-text-answer_201}", "{tag_q-text-answer_202}", "{tag_q-text-answer_203}", "{tag_q-text-answer_204}", "{tag_q-multichoice-answer_2}{tag_q-t/f-answer_2}", "{tag_q-visual-reference_2}", "{tag_q-youtube_2}{tag_q-vimeo_2}{tag_q-image_2_value}", "{tag_q-next-question_2}", "CAT_Custom_19", "CAT_Custom_20", "CAT_Custom_22", "CAT_Custom_23", "CAT_Custom_24", "CAT_Custom_25", "CAT_Custom_21", "CAT_Custom_26", "CAT_Custom_27", "CAT_Custom_28", "CAT_Custom_29");
question[3] = new Question("3", "{tag_q-question_3}", "{tag_q-answer-type_3}", "{tag_q-text-answer_301}", "{tag_q-text-answer_302}", "{tag_q-text-answer_303}", "{tag_q-text-answer_304}", "{tag_q-multichoice-answer_3}{tag_q-t/f-answer_3}", "{tag_q-visual-reference_3}", "{tag_q-youtube_3}{tag_q-vimeo_3}{tag_q-image_3_value}", "{tag_q-next-question_3}", "CAT_Custom_30", "CAT_Custom_31", "CAT_Custom_33", "CAT_Custom_34", "CAT_Custom_35", "CAT_Custom_36", "CAT_Custom_32", "CAT_Custom_37", "CAT_Custom_38", "CAT_Custom_39", "CAT_Custom_40");

for (var i = 1; i <= 3; i++) {
    if (question[i].nextQ === "Yes") {
        question[i].createQuestion();
    } else {
        question[1].createQuestion();
    };
}   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top