Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(function() {
- window.questionIndex = 0;
- window.results = [];
- if (document.cookie.indexOf("question-index=") >= 0) {
- window.questionIndex = parseInt(Cookies.get('question-index'));
- }
- if (document.cookie.indexOf("results") >= 0) {
- window.results = JSON.parse(Cookies.get("results"));
- }
- if ($('#login-form').length > 0) {
- /* login form available, must be index.html */
- $('#login-form').submit(function(e) {
- var userName = $('.input-username').val();
- if (userName.length == 0) {
- alert("Please enter a name longer than 0 characters.");
- e.preventDefault();
- }
- Cookies.set("username", userName);
- });
- }
- else {
- /* not available, must be question-page.html */
- $.getJSON("questions.json", function(data){
- window.questions = data;
- $('.question-number').text(
- "Question " + (parseInt(window.questionIndex) + 1) +
- " of " + window.questions.length
- );
- var question = window.questions[window.questionIndex];
- $('.question-image').attr("src", question.image);
- $('.question-section > p').text(question.text);
- var inputArea = $('.input-area');
- inputArea.html("");
- for (var i=0; i<question.answers.length; i++) {
- var answer = question.answers[i];
- inputArea.append(
- '<button type="button" class="answer-button" onclick="answer(' + i + ');">' +
- answer.text +
- '</button>'
- );
- }
- });
- }
- window.answer = function(answerIndex) {
- var question = window.questions[window.questionIndex];
- var answer = question.answers[answerIndex];
- /* save our result onto the array */
- window.results.push(answerIndex);
- Cookies.set('results', window.results);
- /* If questionIndex + 1 is higher than the number of questions, got end page. */
- if (window.questionIndex + 1 >= 2) {
- alert("you picked " + answer.text);
- location.href = "results.html";
- } else {
- Cookies.set('question-index', window.questionIndex + 1);
- alert("you picked " + answer.text);
- location.reload();
- }
- };
- window.resetQuestions = function() {
- Cookies.remove("username");
- Cookies.remove('results');
- Cookies.remove('question-index');
- location.href = "index.html";
- };
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement