Pen Settings

HTML

CSS

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.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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.

+ add another resource

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

              
                <h1>Javascript Quiz Game</h1>
<div class="quiz-container">
  <div id="quiz"></div>
 
</div>
<button id="previous">Previous Question</button>
<button id="next">Next Question</button>
<button id="submit">Submit Quiz</button>
<div id="results"></div> 



</div>
              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Work+Sans:300,600);

body{
	font-size: 19px;
	font-family: 'Avenir', sans-serif;
	color: #212020;
  font-weight: 300;
  text-align: center;
  background-color: #42f4d7;
}
h1{
  font-weight: 300px;
  margin: 0px;
  padding: 10px;
  font-size: 30px;
  background-color: #000;
  color: #fff;

;
}
.question{
  font-size: 30px;
  margin-bottom: 10px;
}
.answers {
  margin-bottom: 20px;
  text-align: left;
  display: inline-block;
}
.answers label{
  display: block;
  margin-bottom: 15px;
}
button{
  font-family: 'Brandon Grotesque', sans-serif;
	font-size: 15px;
	background-color: #fff;
	color: #212020;
	border: 0px;
	border-radius: 20px;
	padding: 20px;
 cursor: -webkit-grabbing; cursor: grabbing;
	margin-bottom: 20px;
  margin-top: 60px;
}
button:hover{
	background-color: #e5acac;
}

.slide{
  position: absolute;
  left: 0px;
  top: 0px;
  width: 100%;
  z-index: 2;
  opacity: 0;
  transition: opacity 0.3s;
}
.active-slide{
  opacity: 2;
  z-index: 3;
}
.quiz-container{
  position: relative;
  height: 200px;
  margin-top: 40px;
}




              
            
!

JS

              
                (function() {
  const myQuestions = [
    {
      question: "How many fingers are on the foot of a man?",
      answers: {
        a: "one",
        b: "Five",
        c: "Three"
       },
      correctAnswer: "c" 
    
    },
    {
      question: "Whose face was said to have launched 1000 ships?",
      answers: {
        a: "Rowan Atkinson",
        b: "Barbara Mori",
        c: "Helen of Troy"
      },
      correctAnswer: "c"
    },
    {
      question: "Water boils at 212 degrees on which temperature scale?",
      answers: {
        a: "Kelvin",
        b: "Fahrenheit",
        c: "Celsius",
       
      },
      correctAnswer: "b"
    },
    {
      question: "What is the Italian word for pie?",
      answers: {
        a: "Pie",
        b: "Lasagna",
        c: "Pizza",
       
      },
      correctAnswer: "c"
    },
     {
      question: "What was Marilyn Monroe's natural hair colour?",
      answers: {
        a: "Ginger",
        b: "Blue",
        c: "Brown",
       
      },
      correctAnswer: "a"
    },
     {
      question: "Which two metals is pewter made from?",
      answers: {
        a: "Copper and Bronze",
        b: "Tin and lead.",
        c: "Zinc and gold",
       
      },
      correctAnswer: "b"
    },
     {
      question: "What takes place in Hong Kong's Happy Valley?",
      answers: {
        a: "Monkey racing",
        b: "Dog racing.",
        c: "Horse racing",
       
      },
      correctAnswer: "c"
    },
     {
      question: "Which country does the sport of pelato come from?",
      answers: {
        a: "Portugal",
        b: "Spain",
        c: "Italy",
       
      },
      correctAnswer: "b"
    },
     {
      question: "How many sides, in total, would three triangles and three rectangles have?",
      answers: {
        a: "21",
        b: "19",
        c: "12",
       
      },
      correctAnswer: "a"
    },
     {
      question: "Who invented the bikini?",
      answers: {
        a: "Gianni Versace",
        b: "Louis Reard.",
        c: "Dolce Gabbana",
       
      },
      correctAnswer: "b"
    },
  ];

  function buildQuiz() {
    // we'll need a place to store the HTML output
    const output = [];

    // for each question...
    myQuestions.forEach((currentQuestion, questionNumber) => {
      // we'll want to store the list of answer choices
      const answers = [];

      // and for each available answer...
      for (letter in currentQuestion.answers) {
        // ...add an HTML radio button
        answers.push(
          `<label>
             <input type="radio" name="question${questionNumber}" value="${letter}">
              ${letter} :
              ${currentQuestion.answers[letter]}
           </label>`
        );
      }

      // add this question and its answers to the output
      output.push(
        `<div class="slide">
           <div class="question"> ${currentQuestion.question} </div>
           <div class="answers"> ${answers.join("")} </div>
         </div>`
      );
    });

    // finally combine our output list into one string of HTML and put it on the page
    quizContainer.innerHTML = output.join("");
  }

  function showResults() {
    // gather answer containers from our quiz
    const answerContainers = quizContainer.querySelectorAll(".answers");

    // keep track of user's answers
    let numCorrect = 0;

    // for each question...
    myQuestions.forEach((currentQuestion, questionNumber) => {
      // find selected answer
      const answerContainer = answerContainers[questionNumber];
      const selector = `input[name=question${questionNumber}]:checked`;
      const userAnswer = (answerContainer.querySelector(selector) || {}).value;

      // if answer is correct
      if (userAnswer === currentQuestion.correctAnswer) {
        // add to the number of correct answers
        numCorrect++;

        // color the answers green
        answerContainers[questionNumber].style.color = "lightgreen";
      } else {
        // if answer is wrong or blank
        // color the answers red
        answerContainers[questionNumber].style.color = "red";
      }
    });

    // show number of correct answers out of total
    resultsContainer.innerHTML = `${numCorrect} out of ${myQuestions.length}`;
  }

  function showSlide(n) {
    slides[currentSlide].classList.remove("active-slide");
    slides[n].classList.add("active-slide");
    currentSlide = n;
    
    if (currentSlide === 0) {
      previousButton.style.display = "none";
    } else {
      previousButton.style.display = "inline-block";
    }
    
    if (currentSlide === slides.length - 1) {
      nextButton.style.display = "none";
      submitButton.style.display = "inline-block";
    } else {
      nextButton.style.display = "inline-block";
      submitButton.style.display = "none";
    }
  }

  function showNextSlide() {
    showSlide(currentSlide + 1);
  }

  function showPreviousSlide() {
    showSlide(currentSlide - 1);
  }

  const quizContainer = document.getElementById("quiz");
  const resultsContainer = document.getElementById("results");
  const submitButton = document.getElementById("submit");

  // display quiz right away
  buildQuiz();

  const previousButton = document.getElementById("previous");
  const nextButton = document.getElementById("next");
  const slides = document.querySelectorAll(".slide");
  let currentSlide = 0;

  showSlide(0);

  // on submit, show results
  submitButton.addEventListener("click", showResults);
  previousButton.addEventListener("click", showPreviousSlide);
  nextButton.addEventListener("click", showNextSlide);
})();


        
        
   
    
    
    
    
        

    
              
            
!
999px

Console