build a trivia and answer javascript

// code by webdevtrick (https://webdevtrick.com)
function Quiz(questions) {
    this.score = 0;
    this.questions = questions;
    this.questionIndex = 0;
}
 
Quiz.prototype.getQuestionIndex = function() {
    return this.questions[this.questionIndex];
}
 
Quiz.prototype.guess = function(answer) {
    if(this.getQuestionIndex().isCorrectAnswer(answer)) {
        this.score++;
    }
 
    this.questionIndex++;
}
 
Quiz.prototype.isEnded = function() {
    return this.questionIndex === this.questions.length;
}
 
 
function Question(text, choices, answer) {
    this.text = text;
    this.choices = choices;
    this.answer = answer;
}
 
Question.prototype.isCorrectAnswer = function(choice) {
    return this.answer === choice;
}
 
 
function populate() {
    if(quiz.isEnded()) {
        showScores();
    }
    else {
        // show question
        var element = document.getElementById("question");
        element.innerHTML = quiz.getQuestionIndex().text;
 
        // show options
        var choices = quiz.getQuestionIndex().choices;
        for(var i = 0; i < choices.length; i++) {
            var element = document.getElementById("choice" + i);
            element.innerHTML = choices[i];
            guess("btn" + i, choices[i]);
        }
 
        showProgress();
    }
};
 
function guess(id, guess) {
    var button = document.getElementById(id);
    button.onclick = function() {
        quiz.guess(guess);
        populate();
    }
};
 
 
function showProgress() {
    var currentQuestionNumber = quiz.questionIndex + 1;
    var element = document.getElementById("progress");
    element.innerHTML = "Question " + currentQuestionNumber + " of " + quiz.questions.length;
};
 
function showScores() {
    var gameOverHTML = "<h1>Result</h1>";
    gameOverHTML += "<h2 id='score'> Your scores: " + quiz.score + "</h2>";
    var element = document.getElementById("quiz");
    element.innerHTML = gameOverHTML;
};
 
// create questions here
var questions = [
    new Question("Hyper Text Markup Language Stand For?", ["JavaScript", "XHTML","CSS", "HTML"], "HTML"),
    new Question("Which language is used for styling web pages?", ["HTML", "JQuery", "CSS", "XML"], "CSS"),
    new Question("Which is not a JavaScript Framework?", ["Python Script", "JQuery","Django", "NodeJS"], "Django"),
    new Question("Which is used for Connect To Database?", ["PHP", "HTML", "JS", "All"], "PHP"),
    new Question("Webdevtrick.com is about..", ["Web Design", "Graphic Design", "SEO & Development", "All"], "All")
];
 
// create quiz
var quiz = new Quiz(questions);
 
// display quiz
populate();

3.75
4

                                    /** code by webdevtrick (https://webdevtrick.com) **/
body {
&nbsp;&nbsp;&nbsp;&nbsp;background-color: #413D3D;
}
&nbsp;
.grid {
&nbsp;&nbsp;&nbsp;&nbsp;width: 600px;
&nbsp;&nbsp;&nbsp;&nbsp;height: 500px;
&nbsp;&nbsp;&nbsp;&nbsp;margin: 0 auto;
&nbsp;&nbsp;&nbsp;&nbsp;background-color: #fff;
&nbsp;&nbsp;&nbsp;&nbsp;padding: 10px 50px 50px 50px;
&nbsp;&nbsp;&nbsp;&nbsp;border: 2px solid #cbcbcb;
&nbsp;&nbsp;&nbsp;&nbsp;
}
&nbsp;
.grid h1 {
&nbsp;&nbsp;&nbsp;&nbsp;font-family: &quot;sans-serif&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;background-color: #01BBFF;
&nbsp;&nbsp;&nbsp;&nbsp;font-size: 60px;
&nbsp;&nbsp;&nbsp;&nbsp;text-align: center;
&nbsp;&nbsp;&nbsp;&nbsp;color: #ffffff;
&nbsp;&nbsp;&nbsp;&nbsp;padding: 2px 0px;
&nbsp;&nbsp;&nbsp;&nbsp;
}
&nbsp;
#score {
&nbsp;&nbsp;&nbsp;&nbsp;color: #01BBFF;
&nbsp;&nbsp;&nbsp;&nbsp;text-align: center;
&nbsp;&nbsp;&nbsp;&nbsp;font-size: 30px;
}
&nbsp;
.grid #question {
&nbsp;&nbsp;&nbsp;&nbsp;font-family: &quot;monospace&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;font-size: 30px;
&nbsp;&nbsp;&nbsp;&nbsp;color: #01BBFF;
}
&nbsp;
.buttons {
&nbsp;&nbsp;&nbsp;&nbsp;margin-top: 30px;
}
&nbsp;
#btn0, #btn1, #btn2, #btn3 {
&nbsp;&nbsp;&nbsp;&nbsp;background-color: #01BBFF;
&nbsp;&nbsp;&nbsp;&nbsp;width: 250px;
&nbsp;&nbsp;&nbsp;&nbsp;font-size: 20px;
&nbsp;&nbsp;&nbsp;&nbsp;color: #fff;
&nbsp;&nbsp;&nbsp;&nbsp;border: 1px solid #1D3C6A;
&nbsp;&nbsp;&nbsp;&nbsp;margin: 10px 40px 10px 0px;
&nbsp;&nbsp;&nbsp;&nbsp;padding: 10px 10px;
}
&nbsp;
#btn0:hover, #btn1:hover, #btn2:hover, #btn3:hover {
&nbsp;&nbsp;&nbsp;&nbsp;cursor: pointer;
&nbsp;&nbsp;&nbsp;&nbsp;background-color: #01BBFF;
}
&nbsp;
#btn0:focus, #btn1:focus, #btn2:focus, #btn3:focus {
&nbsp;&nbsp;&nbsp;&nbsp;outline: 0;
}
&nbsp;
#progress {
&nbsp;&nbsp;&nbsp;&nbsp;color: #2b2b2b;
&nbsp;&nbsp;&nbsp;&nbsp;font-size: 18px;
}

3.75 (4 Votes)
0
4.33
3
Rsadhvika 130 points

                                    &lt;!DOCTYPE html&gt;
&lt;!-- code by webdevtrick (https://webdevtrick.com) --&gt;
&lt;html&gt;
&lt;head lang=&quot;en&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta charset=&quot;UTF-8&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;Quiz&lt;/title&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;style.css&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;div class=&quot;grid&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;div id=&quot;quiz&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;h1&gt;Quiz in JavaScript&nbsp;&nbsp;Webdevtrick.com&lt;/h1&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;hr style=&quot;margin-bottom: 20px&quot;&gt;
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;p id=&quot;question&quot;&gt;&lt;/p&gt;
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;div class=&quot;buttons&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;button id=&quot;btn0&quot;&gt;&lt;span id=&quot;choice0&quot;&gt;&lt;/span&gt;&lt;/button&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;button id=&quot;btn1&quot;&gt;&lt;span id=&quot;choice1&quot;&gt;&lt;/span&gt;&lt;/button&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;button id=&quot;btn2&quot;&gt;&lt;span id=&quot;choice2&quot;&gt;&lt;/span&gt;&lt;/button&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;button id=&quot;btn3&quot;&gt;&lt;span id=&quot;choice3&quot;&gt;&lt;/span&gt;&lt;/button&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/div&gt;
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;hr style=&quot;margin-top: 50px&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;footer&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;p id=&quot;progress&quot;&gt;Question x of y&lt;/p&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/footer&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/div&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/div&gt;
&nbsp;
&nbsp;
&lt;script src=&quot;question.js&quot;&gt;&lt;/script&gt;
&nbsp;
&lt;/body&gt;
&lt;/html&gt;

4.33 (3 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
build a trivia and answer javascript add new quiz in html page html js quiz game js quiz generate questions simple function to add quiz in javascript html javascript question answer game html javascript questions answer game quiz javascript code quiz tutorial javascript javascript quiz code make quiz html js quiz html code quiz javascript quiz app in javascript how to make a quiz on website html make a javascript quiz how to create quit with one question on one page php show quiz summary in quiz app html, js make a quiz in javascript quiz application in javascript quiz generator in javascript create quiz html code questions creator in html css and javascript how create custom quiz form js create quiz in js online quiz application in html and css js how we can create quiz on website in html js quiz game make javascript question next quiz questions slide in js quiz questions slid in js multiple quiz divs javascript show alert how can you make a series of questions with javascript how to create a quiz in html how to create a trivia game in html simple quiz app in javascript how to make quiz in js javascript application for quiz javascript code for marking quiz quiz code in html and javascript quiz in html and javascript quiz appp javascript make multiple choice questions js makemultiple choice questions js javascript quiz app quiz app javascript simple quiz game javascript object javascript object create questions quiz quiz slide javascript creating a website where people can make their own quiz by adding there questions to a json file add a button that allows the user to add a question to the website Quiz app using js and json add a button that allows the user to add a question to the website Quiz app build js objekt quiz javascript quiz game quiz template in javascript
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source