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">
        <p>Enter one name per line, specify the number of winners, then click "Pick Winners".</p>
        <textarea id="names" placeholder="Enter names, separated by newline"></textarea>
        <input type="number" id="numWinners" min="1" value="2" />
        <button onclick="pickWinners()">Pick Winners</button>
        <button onclick="resetForm()">Reset</button>
        <div id="loading" style="display:none;">Loading...</div>
        <div id="winners"></div>
    </div>
    <script src="script.js"></script>


              
            
!

CSS

              
                body {
    font-family: Arial, sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f0f0f0;
}

#container {
    background-color: #fff;
    padding: 20px;
    border-radius: 5px;
    box-shadow: 0 0 10px rgba(0,0,0,0.1);
}

textarea {
    width: 100%;
    height: 100px;
}

button {
    display: block;
    margin: 10px 0;
}

#winners {
    font-weight: bold;
}

#tags-input {
    display: flex;
    flex-wrap: wrap;
    border: 1px solid #ccc;
    padding: 5px;
    cursor: text;
}

#tags-input input {
    border: none;
    outline: none;
}

              
            
!

JS

              
                // Fisher-Yates Shuffle Algorithm
function shuffle(array) {
    var currentIndex = array.length, temporaryValue, randomIndex;
    while (0 !== currentIndex) {
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;
        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
    }
    return array;
}

// Check for duplicate names
function checkDuplicates(names) {
    var nameSet = new Set(names);
    if (nameSet.size !== names.length) {
        alert('Duplicate entries found. Duplicates will be ignored.');
        return Array.from(nameSet);
    }
    return names;
}

// Reset the form
function resetForm() {
    document.getElementById('names').value = '';
    document.getElementById('numWinners').value = '2';
    document.getElementById('winners').innerText = '';
}

// Main function to pick winners
function pickWinners() {
    // Get the text from the textarea
    var text = document.getElementById('names').value.trim();
    
    // Validate input
    if (text === '') {
        alert('Please enter some names.');
        return;
    }
    
    // Split the text into an array of names
    var names = text.split('\n').filter(name => name.trim() !== '');
    
    // Check for duplicates
    names = checkDuplicates(names);
    
    // Get the number of winners from the input field
    var numWinners = parseInt(document.getElementById('numWinners').value);
    
    // Ensure the number of winners is valid
    if (isNaN(numWinners) || numWinners <= 0 || numWinners > names.length) {
        alert('Please enter a valid number of winners.');
        return;
    }
    
    // Display loading spinner (assuming you have a div with id 'loading')
    document.getElementById('loading').style.display = 'block';
    
    // Shuffle the array
    var shuffledNames = shuffle(names);
    
    // Pick the winners from the shuffled array
    var winners = shuffledNames.slice(0, numWinners);
    
    // Hide loading spinner
    document.getElementById('loading').style.display = 'none';
    
    // Display the winners
    document.getElementById('winners').innerText = 'Winners: ' + winners.join(', ');
}

              
            
!
999px

Console