I am new to Javascript and Jquery, and am currently working on a trivia quiz project for class with a teammate. Using a pre-written JQuery code, I have to retrieve data from an external text file created by a teammate, that was written in SQL format, and pass that data as question and answers for the quiz . I wanted to know what is the best way to retrieve the data.
This is the current iteration of the Jquerycode, and has been trimmed to show the array holding a question, and the functions to get the question and answers.
$(function () {
var questions = [{
question: "What is 2*5?",
choices: [2, 5, 10, 15, 20],
correctAnswer: 2
}, {
question: "What is 3*6?",
choices: [3, 6, 9, 12, 18],
correctAnswer: 4
},
}];
// Display initial question
displayNext();
// Creates and returns the div that contains the questions and
// the answer selections
function createQuestionElement(index) {
var qElement = $('<div></div>', {
id: 'question'
});
var header = $('<h2>Question ' + (index + 1) + ':</h2>');
qElement.append(header);
var question = $('<p>').append(questions[index].question);
qElement.append(question);
var radioButtons = createRadios(index);
qElement.append(radioButtons);
return qElement;
}
// Creates a list of the answer choices as radio inputs
function createRadios(index) {
var radioList = $('<ul>');
var item;
var input = '';
for (var i = 0; i < questions[index].choices.length; i++) {
item = $('<li>');
input = '<input type="radio" name="answer" value=' + i + ' />';
input += questions[index].choices[i];
item.append(input);
radioList.append(item);
}
return radioList;
}
})();
This is the text file with one question, there are multiple in the real file.
create table TriviaQuestion
{
questionNum int,
question VARCHAR(255),
option1 VARCHAR(255),
option2 VARCHAR(255),
option3 VARCHAR(255),
option4 VARCHAR(255),
option5 VARCHAR(255),
questionAnswer VARCHAR(100);
CONSTRAINT [PK_questionNum]
}
Insert into TriviaQuestion(questionNum,question,option1,option2,option3,option4,option5,questionAnswer)
Values('1','Which list contains words that are NOT names of shoe types:',
'A. Oxford, jelly, boat, clogs, stiletto, mary jane',
'B. Loafer, gladiator, wedge, mule, platform',
'C. Pump, moccasin, wingtip, sneaker, derby, monk',
'D. Chalupa, dogler, hamster, croonley, frankfurt',
'',
'D. Chalupa, dogler, hamster, croonley, frankfurt')
;