// This is the text editor interface.
// Anything you type or change here will be seen by the other person in real time.

// concurrency - how many tasks we can run at a time - integer
function TaskRunner(concurrency) {
    this.capacity = concurrency;
    this.state = "empty";
    this.count = 0;
    this.storage = [] // turn to q
}


TaskRunner.prototype.push = function push( task ) {
    var that = this;

    if( this.state >= this.capacity ){
        console.log( "executes immediately" );
        this.storage.push( task );
        task(cb);
        this.count += 1
    }
    else{
        console.log( "should wait until one of the running tasks completes" );
        this.storage.push( task );
    }

    function cb(){
      // return that.count -= 1;
      console.log("cb: ", that.count);
      that.count -= 1;
      // that.storage[that.count];

      // that.storage[that.count]();
      // if( that.storage[0] ){
      //   return that.storage[0]()
      //   that.storage = that.storage.slice(1);
      // }
    };// cb

}// push

function exampleSimpleTask(done) {
  console.log("running");
  setTimeout(done, Math.random() * 1000);
}

var r = new TaskRunner(3);
// use the exampleSimpleTask from above;

r.push(exampleSimpleTask); // executes immediately
r.push(exampleSimpleTask); // executes immediately
r.push(exampleSimpleTask); // executes immediately
r.push(exampleSimpleTask); // should wait until one of the running tasks completes
r.push(exampleSimpleTask); // should wait until one of the running tasks completes


@@ -313,16 +313,17 @@
 age = %5B%5D
+;
  // turn
@@ -329,16 +329,17 @@
 n to q%0A%7D
+;
 %0A%0A%0ATaskR
@@ -424,16 +424,15 @@
 his.
-state %3E=
+count %3C
  thi
@@ -496,43 +496,8 @@
  );%0A
-        this.storage.push( task );%0A
     
@@ -733,145 +733,30 @@
     
-console.log(%22cb: %22, that.count);%0A      that.count -= 1;%0A      // that.storage%5Bthat.count%5D;%0A%0A      // that.storage%5Bthat.count%5D();%0A      //
+that.count -= 1;%0A     
  if(
@@ -785,19 +785,9 @@
     
-//   return
+ 
  tha
@@ -800,25 +800,23 @@
 age%5B0%5D()
+;
 %0A     
- //
    that.
@@ -858,12 +858,10 @@
     
-// 
 %7D
+%0A
 %0A   
@@ -871,16 +871,17 @@
 // cb%0A%0A%7D
+;
 // push%0A
@@ -913,17 +913,16 @@
 sk(done)
- 
 %7B%0A  cons
@@ -984,16 +984,17 @@
 1000);%0A%7D
+;
 %0A%0Avar r 

add solution