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

              
                <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Chat Quiz</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    <style>
        /* Add your custom styles here */
        .avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            margin-right: 10px;
        }

        /* Add styles for the chat messages */
        .message {
            background-color: #f2f2f2;
            padding: 10px;
            margin-bottom: 10px;
            border-radius: 10px;
            box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
            opacity: 0;
            transition: opacity 2s, transform 2s;
        }
    </style>
</head>
<body>
<div class="container mt-5">
    <div class="card">
        <div class="card-header">
            Chat Quiz
        </div>
        <div class="card-body" id="chat-box">
            <!-- Chat messages will be displayed here -->
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
    // JavaScript logic for handling the chat quiz
    const chatBox = document.getElementById('chat-box');


    // Function to scroll to the latest message
    function scrollToLatestMessage() {
        chatBox.scrollTop = chatBox.scrollHeight;
        window.scrollTo({ left: 0, top: document.body.scrollHeight, behavior: "smooth" });

    }

    function displayMessage(message, sender, isImage = false) {
        const messageDiv = document.createElement('div');
        messageDiv.classList.add('message');
        messageDiv.style.opacity = '0'; // Set initial opacity to 0

        if (isImage) {
            messageDiv.innerHTML = `<img src="${message}" alt="Choice" class="avatar ${sender}">(Image choice)`;
        } else {
            messageDiv.innerHTML = `<img src="https://i.pravatar.cc/300" alt="Avatar" class="avatar ${sender}">${message}`;
        }

        chatBox.appendChild(messageDiv);

        // Apply a small delay before adding the message to ensure the fade-in effect
        setTimeout(() => {
            messageDiv.style.opacity = '1'; // Set opacity to fully visible after the delay
            scrollToLatestMessage(); // Scroll to the latest message
        }, 10);
    }

    function displayAnswerMessage(message, sender, isImage = false) {
        const messageDiv = document.createElement('div');
        messageDiv.classList.add('message');
        messageDiv.classList.add('text-right');
        messageDiv.style.opacity = '0'; // Set initial opacity to 0

        if (isImage) {
            messageDiv.innerHTML = `  (Image choice)`;
        } else {
            messageDiv.innerHTML = `  ${message}`;
        }

        chatBox.appendChild(messageDiv);

        // Apply a small delay before adding the message to ensure the fade-in effect
        setTimeout(() => {
            messageDiv.style.opacity = '1'; // Set opacity to fully visible after the delay
            scrollToLatestMessage(); // Scroll to the latest message
        }, 10);
    }

    function checkAnswer(answer) {
        // Implement your quiz logic here
        if (currentQuestion === 1) {
            if (answer.toLowerCase() === 'a') {
                // displayMessage('You selected option A', 'system');
                currentQuestion=2
                displayQuestionAndAnswers('2e  question.', question2Choices)
            } else if (answer.toLowerCase() === 'b') {
                displayMessage('You selected option B', 'system');
            } else if (answer.toLowerCase() === 'c') {
                displayMessage('You selected option C', 'system');
            } else {
                displayMessage('Incorrect answer. Try again.', 'system');
            }
        } else if (currentQuestion === 2) {
            // Implement logic for the second question
            if (answer.toLowerCase() === 'x') {
                displayMessage('You selected option X', 'system');
            } else if (answer.toLowerCase() === 'y') {
                displayMessage('You selected option Y', 'system');
            } else if (answer.toLowerCase() === 'z') {
                displayMessage('You selected option Z', 'system');
            } else {
                displayMessage('Incorrect answer. Try again.', 'system');
            }
        }

        // Scroll to the latest message with a smooth effect
         scrollToLatestMessage ();
    }

    // Generate JavaScript-coded choices for the first question
    const question1Choices = ['A', 'B', 'C'];
    const question2Choices = ['X', 'Y', 'Z'];

    function displayQuestionAndAnswers(question, choices) {
        displayMessage('' + question, 'system');
        const choicesHTML = choices.map((choice) => {
            return `<button onclick="checkAnswer('${choice}')" class="btn btn-outline-primary">${choice}</button>`;
        }).join('');
        displayAnswerMessage(choicesHTML, 'system');
    }

    let currentQuestion = 1;

    displayQuestionAndAnswers('1st QUESTION.', question1Choices);

    
</script>
</body>
</html>

              
            
!

CSS

              
                
              
            
!

JS

              
                
              
            
!
999px

Console