cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

            
              <div>
  <button id="btnCreateProgressBar" data-number-of-bars="0">
    Create New Progress Bar
  </button>
</div>

<div id="progressBarContainer">
</div>
            
          
!
            
              .barControl {
  width: 30px;
  height: 20px;
}
            
          
!
            
              /*******************************************************************************
Technical Interview
Create Dynamic Progress Bars

Robert Sogomonian
Dec 18, 2015

Updated: Mar 12, 2016

1. Create a progress bar that fills from 0% - 100% in 5 seconds.
2. Add buttons that control starting, stopping, and resetting the progress bar.
3. Create button that handles creating new progress bars each time it is pressed.
   Each progress bar should have its own control buttons, and no more than three
   progress bars should be able to be created.
*******************************************************************************/

(function(window, undefined){
  // 
  var btnCreateProgressBar = document.getElementById('btnCreateProgressBar'),
      divProgressBarContainer = document.getElementById('progressBarContainer'),
      maxAllowedProgressBars = 3;
  
  
  // When button is clicked, add a new progress bar to the page
  btnCreateProgressBar.addEventListener("click", function () {
    var progressBarCount = btnCreateProgressBar.dataset.numberOfBars;
    
    // If we don't already have a full set of progress bars
    if (progressBarCount < maxAllowedProgressBars) {
      var container = createNewProgressBar(progressBarCount);
      
      // Create controls
      addControl("start", progressBarCount, container);
      addControl("stop", progressBarCount, container);
      addControl("reload", progressBarCount, container);
      
      // Increment count of progress bars currently on the page
      btnCreateProgressBar.dataset.numberOfBars++;
    }
  });
  
  function createNewProgressBar(barId) {
    // Create a new progress bar and container
    var progressBarDiv = document.createElement("div"),
        newProgressBar = document.createElement("progress");

    // Create a container and progress bar to add to the page
    progressBarDiv.setAttribute("class", "progressContainer");
    newProgressBar.setAttribute("id", "progressBar" + barId);
    newProgressBar.setAttribute("value", "0");
    newProgressBar.setAttribute("max", "100");
    
    // Append all new elements to the page
    divProgressBarContainer.appendChild(progressBarDiv);
    progressBarDiv.appendChild(newProgressBar);
    
    return(progressBarDiv);
  }
  
  function addControl(type, barId, container) {
    var controlElement = document.createElement("button"),
        textLabel = "";
    
    controlElement.appendChild(document.createTextNode(""));
    controlElement.setAttribute("id", type + barId);
    controlElement.setAttribute("class", "barControl");
    container.appendChild(controlElement);
    
    // Add Controller Text and Functionality Depending on Button Type
    switch(type) {
      case 'start':
        controlElement.innerHTML = "&#9658;"; // Play Arrow
        setStartController(controlElement, barId);
        break;
      case 'stop':
        controlElement.innerHTML = "\u2016"; // Pause Bars
        setStopController(controlElement, barId);
        break;
      case 'reload':
        controlElement.innerHTML = "&#8635;"; // Reset Circular Arrow
        setResetController(controlElement, barId);
        break;
    }
  }
  
  function setStartController(element, id) {
    var progressBar = document.getElementById("progressBar"+id),
        startButton = document.getElementById("start"+id);
      element.addEventListener("click", function() {
        // If a timer has not already been set on this button
        if(!startButton.dataset.timerId) {
          startButton.dataset.timerId = setInterval(function() {
            if(progressBar.value < progressBar.max) {
              progressBar.value += 1;
            } else {
              clearInterval(startButton.dataset.timerId);
              delete startButton.dataset.timerId;
            }
          }, 50);
        }
      });
  }
  
  function setStopController(element, id) {
    var startButton = document.getElementById("start"+id);
    element.addEventListener("click", function() {
      clearInterval(startButton.dataset.timerId);
      delete startButton.dataset.timerId;
    });
  }
  
  function setResetController(element, id) {
    var progressBar = document.getElementById("progressBar"+id),
        startButton = document.getElementById("start"+id);
    element.addEventListener("click", function() {
      clearInterval(startButton.dataset.timerId);
      delete startButton.dataset.timerId;
      progressBar.value = 0;
    });
  }
})(this);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console