Skip to content

Instantly share code, notes, and snippets.

@damc-dev
Created June 26, 2014 17:17
Show Gist options
  • Save damc-dev/d3a4a1491fca6c8e2182 to your computer and use it in GitHub Desktop.
Save damc-dev/d3a4a1491fca6c8e2182 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Test Class Object in Javascript" />
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
</body>
</html>
var Test = function() {
var course = '',
id = '',
chapter = '',
title = '',
questions = [];
return {
getCourseName: function() { return courseName; },
setCourseName: function(courseName) { course = courseName; },
getCourseId: function() { return id; },
setCourseId: function(courseId) { id = courseId; },
getChapter: function() { return chapter; },
setChapter: function(courseChapter) { chapter = courseChapter; },
getTestTitle: function() { return title; },
setTestTitle: function(testTitle) { title = testTitle; },
getQuestions: function() { return questions;},
addQuestion: function(questionText, optionsArray) {
var q = {};
q.question = questionText;
q.options = optionsArray;
questions.push(q);
},
addAnswer: function(questionText, questionAnswer) {
questions.forEach( function(q) {
if( q.question == questionText ) {
q.answer = questionAnswer;
}
});
},
getTestObject: function() {
var test = {};
test.course = course;
test.id = id;
test.chapter = chapter;
test.title = title;
test.questions = questions;
return test;
}
};
};
var test = new Test();
test.setCourseName("Marketing");
for(i=0; i<= 5; i++) {
test.addQuestion("question " + i, ["opt1", "opt2"]);
}
console.log(test.getTestObject());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment