Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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

              
                <div id='container'>
<!--Quiz Title, not controlled by the javascript source-->
	<h1>Dynamic Quiz Project</h1>
<!--Dynamic Quiz holder, each div with a button class being a controller-->
	<div id='quiz'></div>
   <button class='button' id='next'>Next</button>
   <a href='#'><div class='button' id='prev'>Prev</div></a>
   <a href='#'><div class='button' id='start'>Start Over</div></a>
</div>
              
            
!

CSS

              
                /* Universal style setting */
html, body { background-color: #CDCDCD; 
		     font-family: Georgia, "Times New Roman", Times, serif;
			 margin: 0;
			 padding: 0; }
			 		
/* Title decoration */
h1 { text-align: center;
	 text-decoration: underline; }
	 
/* Dynamic quiz text and initial display */
#quiz { display: none;
		font-family: Arial, Helvetica, sans-serif; }

/* Navigaion button design*/	 
.button { border: 2px solid #A95211;
    	  border-radius: 3px;
    	  padding: 3px 5px;
    	  float: right;
    	  background-color: #DCDCDC;
    	  margin-left: 4px;
		  display: none;
		  color: #765303;
		  text-decoration: none;
       box-shadow: 1px 1px 2px #643; }

/* Mouse hovering effects for navigation button*/
.button:hover { background-color: #F8F8FF;
    			color: #525252; }

/* Dynamic Quiz container */
#container { width: 400px;
    		 margin: 20px auto;
    		 padding: 0 25px 50px 25px;
    		 background-color: #FD9331;
    		 border: 3px solid #A95211;
    		 border-radius: 3px;
    		 color: #FEFEFE;
    	     font-weight: bold;
    		 box-shadow: 5px 5px 10px #643; }

/* List settings */
ul { list-style-type: none;
     padding: 0;
     margin: 0; }
              
            
!

JS

              
                (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 },
				 { question: "What is 8*9?",
				   choices: [72, 99, 108, 134, 156, 199, 232, 444, 567, 681],
				   correctAnswer: 0 },
				 { question: "What is 1*7?",
				   choices: [4, 5, 6, 7, 8],
				   correctAnswer: 3 },
				 { question: "What is 8*8?",
				   choices: [20, 30, 40, 50, 64, 77, 99],
				   correctAnswer: 4 }];
var questionCounter = 0;
var selections = []; 	

function createQuestionElement(Q) {
  var qElement = $('<div>', { id: 'question' });
  var header = $('<h2>Question ' + (Q+1) + ':</h2>');  		 	 
    qElement.append(header);						
  var question = $('<p>').append(questions[Q].question);	
    qElement.append(question);					
 	qElement.append(createRadios(Q));		
  return qElement; 									
}								

function createRadios(index) {  			
  var radioList = $('<ul>');						
  for (var i = 0; i < questions[index].choices.length; i++) {	
    var item = $('<li>');	
    var input = '<input type="radio" name="answer" value='+i+' />'
			input += questions[index].choices[i];
     item.append(input);						
    radioList.append(item);  					  }
  return radioList;  								
}

function displayNext() {  		
 $('#quiz').fadeOut(function() {	
  $('#question').remove();					
  if(questionCounter < questions.length) {
    $("#quiz").append(createQuestionElement(questionCounter)).fadeIn();
	 $('#next').fadeIn();	
   if(!(isNaN(selections[questionCounter]))) {	
$('input[value='+selections[questionCounter]+']').prop('checked', true);
   }											  
    if(questionCounter > 0) {		
     $('#prev').fadeIn();	
     } 
    else {
     $('#prev').hide();		
     }
    }
   else {
    $('#quiz').append(displayScore()).fadeIn();   
    $('#next, #prev').hide();				
    $('#start').fadeIn();						
  }
 });
}
    
$('#next').on('click', function(event) { 	
  event.preventDefault();
  if($("#quiz").is(':animated')) {    	
    return false;									
  }
  selections[questionCounter] = $('input:checked').val();										//Execute the choose funciton when the 'next' button is clicked
    
  if(isNaN(selections[questionCounter])) {  		//If the user does not make a selection on any question they 		
  													//are on
    alert('Please make a selection!');				//An alert is displayed to the user
  }
  else {											//Otherwise
    questionCounter++;								//Move onto the next question
    displayNext();									//Execute the displayNext function again
  }
});
  
$('#prev').on('click', function(event) {			//Click handler for the 'prev' button
  if($("#quiz").is(':animated')) {					//If the quiz element is in the animation process
    return false;									//Suspend the clickability on the next button
  }
  questionCounter--;								//Move back to the previous question
  displayNext();									//Execute the displayNext function
});
  
$('#start').on('click', function(event) {			//Click handler for the 'Start Over' button
  if($("#quiz").is(':animated')) {					//If the quiz element is in the animation process
    return false;									//Suspend the clickability on the next button
  }
  questionCounter = 0;								//Resets the questionCounter back to 0
  selections = [];									//Resets the selections variable array
  displayNext();									//Executes the displayNext function
  $('#start').fadeOut();							//Fades out the 'Start Over' button
});
  
function displayScore() { 							//Displays the users final score
  var score = $('<p>',{id: 'question'});			//Shortcuts a variable to produce a paragraph tag with a 
  													//#question id
  var numCorrect = 0;								//Sets a variable to 0
  for (var i = 0; i < selections.length; i++) {		//A for loop starting from zero, and increasing in value by one
  													//until it's less than the number of selections
    if (selections[i] === questions[i].correctAnswer) {
													//If the selection number matches the correctAnswer number in
													//the corresponding qustion
      numCorrect++;									//Increase the numCorrect variable by one
    }
  }
  score.append('You got ' + numCorrect + ' questions out of ' + questions.length + ' right!!!');
  													//Append the string with included variables to the score 
													//variable
  return score;										//Compiles score and all appended variables
}  
displayNext(); 										//Executes the displayNext function (Starts the quiz)
}
)();
              
            
!
999px

Console