0

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')

;
Duraigo
  • 19
  • 3
  • 1
    This looks like an X-Y problem to me: you shouldn't be using JS to parse SQL commands stored in a text file. The correct way is to actually execute the SQL commands (once is enough, unless you always have new incoming data) so that it writes the data into a table. Then you setup an API endpoint that fetches the data, and let your jQuery consume whatever data that the API endpoint returns. – Terry Aug 06 '17 at 21:22
  • As Version Zero it looks not very bad. Some tips. Create two tables (1) questions, (2) answers (and link them via `primary key` - `foreign key`) . Use AJAX request to retrieve (random) question from the server. Use AJAX `post` to send user answer to the server. Never send data like `correctAnswer:true` to the client. – Alex Kudryashev Aug 06 '17 at 21:25
  • Look into [this](https://stackoverflow.com/questions/14446447/javascript-read-local-text-file) old but good answered thread – user8393645 Aug 06 '17 at 21:40

0 Answers0