Click here to Skip to main content
15,567,466 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am a beginner to JavaScript and am currently working on a survey project. I have been getting this error: 

try2js.js:26 Uncaught TypeError: Cannot read properties of undefined (reading 'length')
    at MultipleChoiceQuestionOther.MultipleChoiceQuestion.html (try2js.js:26:45)
    at MultipleChoiceQuestionOther.html (try2js.js:40:30)
    at QuestionSection.html (try2js.js:95:36)
    at Survey.html (try2js.js:106:35)
    at <anonymous>:1:8

I've checked that everything is passed correctly, but I can't figure out the issue for the life of me! Any guidance or advice would be greatly appreciated...thank you!

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

       function Question(title, text, options) {
          this.title = title;
          this.text = text;
          this.options = options;
          this.html = function() {
            var result = "";
    
            result += "<h3>" + this.title + "</h3>\n";
            result += "<p>" + this.text + "</p>\n";
    
            return result;
          }
        }

    function MultipleChoiceQuestion(title, text, options) {
      Question.call(this, title, text);

      var superToHTML = this.html;

      this.html = function() {
        var result = superToHTML.call(this);

        result += '<ol type="a">\n';
        for (var counter = 0; counter < options.length; counter++) {
          result += '<li><input type="radio" name="' + title + '" value="' + options[counter] + '">' + options[counter] + '</li>\n';
        }
        result += "</ol>\n";
        return result;
      }
    }

    function MultipleChoiceQuestionOther(title, text, options) {
      MultipleChoiceQuestion.call(this, title, text, options);

      var superToHTML = this.html;

      this.html = function() {
        var result = superToHTML.call(this);

        result += '<p>Other: <input type="text" name="Other"></p>\n';
        return result;
      }
    }

    function MultipleSelectQuestion(title, text, options) {
      Question.call(this, title, text);

      var superToHTML = this.html;

      this.html = function() {
        var result = superToHTML.call(this);

        result += '<ol type="a">\n';
        for (var counter = 0; counter < options.length; counter++) {
          result += '<li><input type="checkbox" name="' + title + '" value="' + options[counter] + '">' + options[counter] + '</li>\n';
        }
        result += "</ol>\n";
        return result;
      }
    }

    function MultipleSelectQuestionOther(title, text, options) {
      MultipleSelectQuestion.call(this, title, text, options);

      var superToHTML = this.html;

      this.html = function() {
        var result = superToHTML.call(this);

        result += '<p>Other: <input type="text" name="Other"></p>\n';
        return result;
      }
    }

    function ShortAnswerQuestion(title, text) {
      Question.call(this, title, text);

      var superToHTML = this.html;

      this.html = function() {
        var result = superToHTML.call(this);

        result += '<p>Please type your answer here: <input type="text" name="' + title + '"></p>\n';
        return result;
      }
    }

    function QuestionSection(title, questions) {
      this.html = function() {
        var result = "<h2>" + title + "</h2>\n";

        for (var counter = 0; counter < questions.length; counter++) {
          result += questions[counter].html();
        }
        return result;
      }
    }

    function Survey(title, sections) {
      this.html = function() {
        var result = "<h1>" + title + "</h1>\n";

        for (var counter = 0; counter < sections.length; counter++) {
          result += sections[counter].html();
        }
        return result;
      }
    }

    function createSurveyQuestions() {
      var q1 = new MultipleChoiceQuestionOther(
        "Gender",
        "What is your gender?" [
          "Male",
          "Female",
          "I do not identify with a gender",
          "Prefer not to answer"
        ]
      );
      var s1 = new QuestionSection("Demographics", [q1]);
      return new Survey("Housing Insecurity Survey", [s1]);
    }

<!-- language: lang-html -->

     <!doctype html>
        <html>
    
        <head>
          <title></title>
          <script src="try2js.js"></script>
        </head>
    
        <body>
          <div id="survey"></div>
        </body>
    
        </html>

<!-- end snippet -->


What I have tried:

I've made sure I passed all variables correctly and checked syntax
Posted
Updated 14-May-22 12:04pm

1 solution

Your JavaScript method expects parameter options when you call:
MultipleChoiceQuestion(title, text, options)

Based on the error and possibly the calling point createSurveyQuestions, you are not defining options or passing it. When MultipleChoiceQuestion is executed, a line of code comes like these:
JavaScript
for (var counter = 0; counter < options.length; counter++)

Probably, you are missing a commma post between defining the questions and adding options:
var q1 = new MultipleChoiceQuestionOther(
        "Gender",
        "What is your gender?" [
          "Male",
          "Female",
          "I do not identify with a gender",
          "Prefer not to answer"
        ]
      );

Try:
var q1 = new MultipleChoiceQuestionOther(
        "Gender",
        "What is your gender?", 
        [
          "Male",
          "Female",
          "I do not identify with a gender",
          "Prefer not to answer"
        ]
      );

Believe a simple Javascript debugging should have pointed the same to you. Try out!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900