0

So for a school assignment I have to make an interactive quiz with javascript. That I did, the problem is that it is required for me to store all the questions in an external text file on local storage. My questions are stored in an array of objects, and I add them to radio buttons with a 'for'-loop

I googled it, but I didn't find an answer that made it clear enough for me... I understand that I can read files out, but I have no idea how I would add the questions to my existing for-loops

I'm still totally new to JS, and have no idea how to implement this

here's a code snippet, where i take the questions out of the array, and add them to the list with radio buttons

var i = 0;
var len = allQuestions.length;

function frageStellen(i){
var anzahlVarianten = allQuestions[i].choices.length;


    for(var j = 0; j < anzahlVarianten; j++){


    //create radio buttons

    var option = document.createElement("li");
    var variante = document.createElement("input");
    variante.setAttribute('type', 'radio');
    variante.setAttribute('name', 'gestellteFrage'+ i);
    option.setAttribute('class', '.option');
    //fragen-Inhalt
    var inhalt = document.createTextNode(allQuestions[i].choices[j]);


    myOptions.appendChild(option);
    option.appendChild(variante);
    option.appendChild(inhalt);
    myQuestion.innerHTML = allQuestions[i].question;
    }
}

Here is the link to the full code: http://jsfiddle.net/7HaCz/

please help!

  • When you say "local storage" do you mean a file on the local hard drive? The way localStorage (name used in Chrome) works is that it will remember information related to a page. E.g. If you do `localStorage['a'] = 10`: chrome will now have a variable retrievable as localStorage['a'] even if you close and re-open that page. – Rhyono Oct 27 '13 at 23:54

1 Answers1

0

Put this part of your code:

var jsonData = [{
  question: "What is Javascript?",
  choices: ["An ancient language", "A programming language", "A medieval manuscript", "An internet troll"],
  correctAnswer: 1
}, {
  question: "Where is Venice?",
  choices: ["Peru", "Greece", "US", "Italy", "Congo"],
  correctAnswer: 3
}, {
  question: "What does RGB mean?",
  choices: ["Red, Green, Blue", "Real Graphics Body", "SWAG"],
  correctAnswer: 0
}, {
  question: "How many strings has a  guitar?",
  choices: [3, 4, 5, 6, 7, 8],
  correctAnswer: 3
}];

In a separate file, then follow this stackoverflow to JSON.parse(jsonData) to get the JSON data out of it again.

Community
  • 1
  • 1
mitchfuku
  • 309
  • 1
  • 2
  • 7