cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <h1>JS Quiz</h1>
<div class="quiz-wrapper">
  <form id="quiz">
    <div class="quiz-navigation">
      <input type="submit" id="prev" value="prev">
      <input type="submit" id="next" value="next">
    </div>
    <!-- /.quiz-navigation -->
  </form>
  <!-- /#quiz -->

</div>
<!-- /.quiz-wrapper -->
            
          
!
            
              //<link href="https://fonts.googleapis.com/css?family=Fira+Sans:800|Roboto:300" rel="stylesheet"> 
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body {
  font-family: 'Roboto', sans-serif;
  font-weight: 300;
  padding-top: 40px;
}
h1, h2, h3, h4, h5, h6 {
  font-family: 'Fira Sans', sans-serif;
  font-weight: 800;
}
h1 {
  text-align: center;
}
.quiz-wrapper {
  margin: 20px auto;
  max-width: 600px;
  padding: 30px 50px;
  border: 1px #000 solid;
}
.answer {
  margin: 10px 0;
  position: relative;
}
.question-wrapper {
  margin-bottom: 50px;
}
.question-wrapper h2 {
  font-size: 20px;
  line-height: 25px;
}
label {
  line-height: 20px;
  vertical-align: middle;
}
button,
input[type="submit"] {
  background-color: transparent;
  border: 1px #000 solid;
  margin: 0 2px;
  padding: 5px 10px;
  cursor: pointer;
}
button:disabled,
input[type="submit"]:disabled {
  background-color: #000;
  color: #fff;
  cursor: initial;
}
input[type="submit"]:disabled {
  background-color: #999;
  border-color: #999;
}
input[type="radio"] + label::before {
  content: "";
  display: inline-block;
  width: 10px;
  height: 10px;
  border: 2px #000 solid;
  padding: 5px;
  border-radius: 50%;
  margin-right: 10px;
  vertical-align: middle;
}
input[type="radio"]:checked + label::before {
  background-color: #000;
  background-clip: content-box;
}
button:focus,
input[type="submit"]:focus,
input[type="radio"]:focus + label::before {
  box-shadow: 0 3px 3px rgba(0,0,200,.5);
}
input[type="radio"] {
  position: absolute;
  visibility: hidden;
}
input,
button {
  font-family: 'Fira Sans', sans-serif;
  font-weight: 800;
}
.result {
  text-align: center;
}
.result button {
  margin-top: 20px;
  padding: 10px 30px;
  font-size: 1.5em;
}
            
          
!
            
              var QuestionsArray = [
  {
    question: 'Entomology is the science that studies:',
    answers: [
      'Behavior of human beings',
      'Insects',
      'The origin and history of technical and scientific terms',
      'The formation of rocks'
    ],
    correctAnswer: 1,
  },
  {
    question: 'Galileo was an Italian astronomer who:',
    answers: [
      'developed the telescope',
      'discovered four satellites of Jupiter',
      'discovered that the movement of pendulum produces a regular time measurement',
      'All of the above'
    ],
    correctAnswer: 3,
  },
  {
    question: 'Exposure to sunlight helps a person improve his health because:',
    answers: [
      'the infrared light kills bacteria in the body',
      'resistance power increases',
      'the pigment cells in the skin get stimulated and produce a healthy tan',
      'the ultraviolet rays convert skin oil into Vitamin D'
    ],
    correctAnswer: 3,
  },
  {
    question: '\'OS\' computer abbreviation usually means:',
    answers: [
      'Order of Significance',
      'Open Software',
      'Operating System',
      'Optical Sensor'
    ],
    correctAnswer: 2,
  },
  {
    question: 'Which scientist discovered the radioactive element radium?',
    answers: [
      'Isaac Newton',
      'Albert Einstein',
      'Benjamin Franklin',
      'Marie Curie'
    ],
    correctAnswer: 3,
  },
  {
    question: 'The great Victoria Desert is located in:',
    answers: [
      'Canada',
      'West Africa',
      'Australia',
      'North America'
    ],
    correctAnswer: 2,
  },
  {
    question: 'Euclid was',
    answers: [
      'Greek mathematician',
      'Contributor to the use of deductive principles of logic as the basis of geometry',
      'Propounded the geometrical theorems',
      'All of the above'
    ],
    correctAnswer: 3,
  },
  {
    question: 'Who invented Dynamite?',
    answers: [
      'Alfred B. Nobel',
      'Sir Alexander Graham Bell',
      'Benjamin Franklin',
      'Thomas Alva Edison'
   
    ],
    correctAnswer: 0,
  }
];
/*
- cały HTML z poziomu JS
*/

var Quiz = function() {
  this.element = document.getElementById('quiz');
  this.navElement = document.getElementsByClassName('quiz-navigation')[0];
  this.quizNav = function QuizNav(navElement, quizElement, quizObj) {
    this.currentQuestion = 0;
    this.nextBtn = document.getElementById('next');
    this.prevBtn = document.getElementById('prev');
    this.numberBtns = [];
    this.showQuestion = function(index) {
      quizObj.questions.forEach(function(q) {
        if (q.position !== index) {
          q.hide();
        }
      });
      quizObj.questions[index].show();
      this.currentQuestion = index;
    };
    this.next = function() {
      this.showQuestion(++this.currentQuestion);
    };
    this.prev = function() {
      this.showQuestion(--this.currentQuestion);
    };
    this.makeNumbers = function() {
      var self = this;
      quizObj.questions.forEach(function(q) {
        var numberBtn = document.createElement('button');
        numberBtn.textContent = q.position + 1;
        numberBtn.addEventListener('click',function() {
          self.showQuestion(q.position);
          self.checkPos();
        });
        self.numberBtns.push(numberBtn);
        navElement.insertBefore(numberBtn,self.nextBtn);

      });
    };
    this.checkPos = function() {
      this.numberBtns.forEach(function(btn) {
        btn.disabled = '';
      });
      this.numberBtns[this.currentQuestion].disabled = true;
      if (this.currentQuestion === 0) {
        this.prevBtn.disabled = true;
      } else {
        this.prevBtn.disabled = '';
      }
      if (this.currentQuestion === (quizObj.questions.length - 1)) {
        this.nextBtn.disabled = true;
        this.endQuizBtn.add();
      } else {
        this.nextBtn.disabled = '';
        this.endQuizBtn.remove();
      }
    };
    this.endQuizBtn = {
      element: document.createElement('button'),
      add: function() {
        var self = this;
        self.element.textContent = 'End quiz';
        navElement.appendChild(self.element);
      },
      remove: function() {
        var self = this;
        if (navElement.lastChild === self.element) {//tutaj pewnie można zrobić lepsze sprawdzenie
          navElement.removeChild(self.element);
        }
      },
      clicked: function() {
        var score = 0;
        quizObj.questions.forEach(function(q) {
          if (q.isAnswerCorrect()) {
            ++score;
          }
        });
        var content = document.createElement('div');
        content.className = 'result';
        var showScore = document.createElement('h2');
        showScore.textContent = 'Your score: ' + score;
        content.appendChild(showScore);
        quizObj.showAnswers();
        navElement.style.display = 'none';
        var tryAgainBtn = document.createElement('button');
        tryAgainBtn.textContent = 'Try again';
        content.appendChild(tryAgainBtn);
        quizElement.appendChild(content);
        tryAgainBtn.addEventListener('click',function() {
            quizObj.reset();
        });
      },
      addEvent: function() {
        var self = this;
        this.element.addEventListener('click',function() {
          self.clicked();
        });
      }
    },
    this.handleEvents = function() {
      var self = this;
      this.nextBtn.addEventListener('click',function() {
        self.next();
        self.checkPos();
      });
      this.prevBtn.addEventListener('click',function() {
        self.prev();
        self.checkPos();
      });
      this.endQuizBtn.addEvent();
    };
  },
  this.quizNavObject = new this.quizNav(this.navElement,this.element,this);
  this.questions = [];
  this.questionElement = function(QuestionObject,parentElement,questionPosition,navElement) {
    this.question = QuestionObject;
    this.position = questionPosition;
    this.element = document.createElement('div');
    this.element.className = 'question-wrapper';
    this.inputs = [];
    this.labels = [];
    this.create = function() {
      var self = this;
      this.element.innerHTML = '<h2>' + (this.position + 1) + '. ' + QuestionObject.question +'</h2>';
      QuestionObject.answers.forEach(function(answer,index) {
        var answerWrapper = document.createElement('div');
        answerWrapper.className = 'answer';
        var input = document.createElement('input');
        input.type = 'radio';
        input.id = questionPosition + '-' + index + '-num';
        input.name = 'question-' + questionPosition;
        input.value = index;
        var label = document.createElement('label');
        label.htmlFor = questionPosition + '-' + index + '-num';
        label.textContent = answer;
        answerWrapper.appendChild(input);
        answerWrapper.appendChild(label);
        self.inputs.push(input);
        self.labels.push(label);
        self.element.appendChild(answerWrapper);
        input.addEventListener('change',function() {
          self.updateAnswer();
        });
      });
    };
    this.answer = null;
    this.updateAnswer = function() {
      //lecę przez wszystkie elementy z tym samym atrybutem name i sprawdzam który zaznaczony
      var self = this;
      this.inputs.forEach(function(input) {
        if (input.checked) {
          self.answer = input.value;
        }
      });
    }
    this.isAnswerCorrect = function() {
      if (this.answer === null) {
        return false;
      }
      return this.question.correctAnswer === Number(this.answer);
    };
    this.showCorrect = function() {
      this.labels[this.question.correctAnswer].style.color = '#090';
    };
    this.hideCorrect = function() {
      this.labels[this.question.correctAnswer].style.color = 'initial';
    };
    this.showUserAnswers = function() {
      if (this.answer) {
        this.labels[this.answer].style.color = '#900';
      }
    };
    this.hideUserAnswers = function() {
      if (this.answer) {
        this.labels[this.answer].style.color = 'initial';
      }
    }
    this.append = function() {
      this.create();
      parentElement.insertBefore(this.element,navElement);
    };
    this.show = function() {
      this.element.style.display = 'block';
    };
    this.hide = function() {
      this.element.style.display = 'none';
    }
  },
  this.makeQuestions = function() {
    var self = this;
    QuestionsArray.forEach(function(q,index) {
      self.questions.push(new self.questionElement(q,self.element,index,self.navElement));
      self.questions[index].append();
    });
  };
  this.init = function() {
    this.makeQuestions();
    this.quizNavObject.showQuestion(0);
    this.quizNavObject.makeNumbers();
    this.quizNavObject.checkPos(this.questions[0]);
    this.quizNavObject.handleEvents();
    this.element.addEventListener('submit',function(e) {
      e.preventDefault();
    });
  };
  this.reset = function() {
    this.questions.forEach(function(q) {
      q.hide();
      q.hideUserAnswers();
      q.hideCorrect();
      q.answer = null;
      q.inputs.forEach(function(inp) {
        inp.disabled = '';
        inp.style.display = 'initial';
        inp.checked = '';
      });
    });
    this.quizNavObject.currentQuestion = 0;
    this.quizNavObject.showQuestion(0);
    this.quizNavObject.checkPos(this.questions[0]);
    var result = document.getElementsByClassName('result')[0];
    this.element.removeChild(result);
    this.navElement.style.display = 'block';
  },
  this.showAnswers = function() {
    this.questions.forEach(function(q) {
      q.show();
      q.showUserAnswers();
      q.showCorrect();
      q.inputs.forEach(function(inp) {
        inp.disabled = true;
        inp.style.display = 'none';
      });
    });
  }
}

var QuizObj = new Quiz();
QuizObj.init();

            
          
!
999px
Loading ..................

Console