Skip to content

Instantly share code, notes, and snippets.

@matthamil
Last active December 9, 2015 23:13
Show Gist options
  • Save matthamil/9c097ddd62e66d314b0d to your computer and use it in GitHub Desktop.
Save matthamil/9c097ddd62e66d314b0d to your computer and use it in GitHub Desktop.
// Question Module
// Modules inheriting from Question:
// Likert, YesNo, Gender, Age
var Question = function(question, type, answerChoices){
var text = question || '';
var questionType = type || '';
var answers = answerChoices || [];
return {
getQuestion: function () {
return text;
},
getQuestionType: function() {
return questionType;
},
getAnswerChoices: function () {
return answers;
},
getRandomAnswer: function () {
var randomNum = Math.floor(Math.random() * (answers.length - 1)) + 1;
return answers[randomNum] || "Question not defined.";
},
printQuestion: function(){
return ("Question: " + text + "\n" +
"Type: " + questionType + "\n" +
"Answer Choices: " + answers + "\n" +
"Your choice: " + this.getRandomAnswer());
}
};
};
var YesNo = function(question){
Question.call(this, question, "Yes/No", ["Yes", "No"]);
};
YesNo.prototype = new Question();
var test = new YesNo("Do you own a cat?");
console.log(test.printQuestion());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment