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.

            
              <html>
 <head>
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
 </head>
 <body>
   
   <div id = "main">
   </div>
 </body>
</html>
            
          
!
            
              body {
  padding: 0;
  margin: 0;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  background: white;

}

.form-group{
  
font-size: 32px;  
  
  
}
            
          
!
            
              //Student names, levels, and genders
var studentNames = [
{name:'alexandra',level:10,gender:'f'},
{name:'josie',level:8,gender:'f',exclude:['nora', 'charlotte']},
{name:'laurel',level:8,gender:'f'},
{name:'bob',level:10,gender:'m'},
{name:'justin',level:10,gender:'m'},
{name:'aaron',level:9,gender:'m'},
{name:'frances',level:10,gender:'f'},
{name:'sally',level:10,gender:'f'},
{name:'claire',level:9,gender:'f'},
{name:'carol',level:9,gender:'f'},
{name:'lauren',level:10,gender:'f'},
{name:'nora',level:10,gender:'f'},
{name:'charlotte',level:10,gender:'f'},
 {name:'camille',level:7,gender:'f'},
{name:'ben',level:10,gender:'m'},           
{name:'dan',level:6,gender:'m'},
{name:'harold',level:10,gender:'m'},
{name:'evelyn',level:8,gender:'f'},
{name:'leigh-ann',level:10,gender:'f'},
{name:'melissa',level:8,gender:'f'},  
{name:'brian',level:9,gender:'m'},
{name:'norm',level:8,gender:'m'},  
]




var Student = function(student) {

        this.name = student.name || '';
        this.level = student.level || 1;
        this.exclude = student.exclude||[];
        this.gender = student.gender;
};

Student.prototype.valueOf = function(){

return this.name;

}


var Group = function(students){

    var names = [];
    students.forEach(function(s){
      names.push(s.name);

    })
    this.names = names;

    this.students = students;

    this.minLevel = function(){

      var minLevel = 9999;
      students.forEach(function(s){
        if (s.level<minLevel){
          minLevel = s.level;
        }

      })
      return minLevel;

    }();
    this.maxLevel = function(){

      var maxLevel = -1111;
      students.forEach(function(s){
        if (s.level>maxLevel){
          maxLevel = s.level;
        }

      })
      return maxLevel;

    }();

    this.hasConflict = function(){

      var hasConflict = false;

      students.forEach(function(s){

          if(_.intersection(names,s.exclude).length!=0){

            hasConflict = true;
              }

      })

      return hasConflict;

    }()
  
  this.metrics = function(){
  //Here is where I define many of the metrics used to evaluate the groups 
    var groupMetrics={};
    
      var minLevel = 9999;
      students.forEach(function(s){
        if (s.level<minLevel){
          minLevel = s.level;
        }

      })
      groupMetrics.minLevel=minLevel;
    
      var maxLevel = -1111;
      students.forEach(function(s){
        if (s.level>maxLevel){
          maxLevel = s.level;
        }

      })
       groupMetrics.maxLevel=maxLevel;
    
    var total = 0;
      students.forEach(function(s){
        total+=s.level;
        
      })
      
      groupMetrics.mean = (total/students.length)
    
    
     var total = 0;
      
      students.forEach(function(s){
      
        total+=Math.abs(s.level-groupMetrics.mean);
                       
      })
    
      groupMetrics.spread = total/students.length;
    
      
    
      groupMetrics.numM = _.filter(students,function(s){ return (s.gender=='m'); }).length;
      groupMetrics.numF = _.filter(students,function(s){ return (s.gender== 'f'); }).length;
    
      groupMetrics.genderBalance = 1 - Math.abs((groupMetrics.numM-groupMetrics.numF)/(groupMetrics.numM+groupMetrics.numF));
    
  
    
    
      return groupMetrics;
    
    
  }()
  
    

}

var Grouping = function(students,sizes){
  this.students = students;
  this.sizes = sizes;
 var groups = [];
var total=0;
sizes.forEach(function(s,n){
total+=s*(n+1);  
   
})  
if(total!=students.length){
  
 console.log("Size mismatch") 
}

  else{
    
  
  var studentsCopy = students.slice();
 
  sizes.forEach(function(s,n){
    if(s!=0){
    for(var i=0;i<s;i++){
     
        
      
      var newGroup = new Group(studentsCopy.splice(0,n+1));
       //console.log(newGroup)
      groups.push(newGroup);
      studentsCopy.slice(0,n+1); 
        
      }
  }
      
    
 
  })
                
 this.groups = groups;   
 this.score = function(){
 var score = 0;
 groups.forEach(function(g){
 
 score += (2*g.metrics.spread - g.metrics.genderBalance +(g.hasConflict?-100:0));
   
   
 }) 
   
   return score;
   
 }()
    
  

}
};

Grouping.prototype.variant = function(){
  
 var currStudents = this.students.slice(); 
 var a = Math.floor(Math.random()*currStudents.length);
 var b = Math.floor(Math.random()*currStudents.length);
 var temp = currStudents[b];
 currStudents[b]=currStudents[a];
 currStudents[a]=temp;
 return new Grouping(currStudents,this.sizes);
  
}

  



var groups = []

for(var i = 0;i<50;i++){
var students = [];
var studentNames = _.shuffle(studentNames);
studentNames.forEach(function(s){

students.push(new Student(s));

});
  
//This is where I define the numbers of each group size. 0 single students, 0 pairs, 6 triples, 1 quad.  
var grouping = new Grouping(students,[0,0,6,1])
groups.push(grouping);
}

groups = groups.sort(function(a,b){
return b.score-a.score;
  
})


var generations = 10;
var spawnSize = 5;
for(var j = 0;j<generations;j++){
groups = groups.slice(0,10);
groups.forEach(function(g){
  for(var k = 0;k<spawnSize;k++){
    groups.push(g.variant());  
    
  }

  
})


groups = groups.sort(function(a,b){
return b.score-a.score;
  
})


//console.log(groups[0]);
  
}




var App = new React.createClass({
  
render: function(){

var groupComponents = [];
  
  

  
return (

<div className = "container">
 <div> 
<GroupingElement groups = {groups[0].groups}/>
  </div>
  <div>

  </div>
  
  </div>

)  
  
  
  
}  
  
  
  
})

var NamesInput = new React.createClass({

  render: function(){
  
  return (
  <div className="form-group">
    <label for="inputNames">Group Member Names</label><br/>
    <input type="text" className="form-control" id = "inputNames" ref = "inputNames"  placeholder="Enter names here..."/>
      <button type="button" className="btn btn-outline-primary btn-lg" ref = "inputNames" >Next</button>
    <hr/>
  </div>
    
  
  
  )
  
}
  
  
  
})


var SetParametersMain = new React.createClass({
  
  
 render:function(){
   
   var studentElements = [];
    var studentObjects =  studentNames.map(function(s,n){
    var randomGender = (Math.random()>=0.5?"m":"f")
      
      return (
     <div className = "col-md-4"> 
      <StudentProfile name = {s.name} index = {n} gender = {s.gender} level = {s.level}/>
      </div>
        )
      
    })
   
   return (
   
   <div className = "row">
       {studentObjects}
   </div>
   
   
   )
   
 }
  
  
  
})

var StudentProfile = new React.createClass({
  
  
 render: function(){
   var isMale = (this.props.gender=='m')?"btn-primary":"";
    var isFemale = (this.props.gender!='m')?"btn-primary":"";
    var femaleString = "btn "+ isFemale+ " btn-sm"; 
   var maleString = "btn "+ isMale+ " btn-sm"
  return ( 
   <div>
  <div className="card" style={{width: "20rem",margin:"5px"}}>
 
  <div className="card-block">
    
    <h4 className="card-title">
      <div>
      <span className="badge badge-pill badge-default"> #{this.props.index+1}</span> {this.props.name}
      </div>
    </h4>
    <p className="card-text">
    <ul>
      <li>Level: <input type = "number" min = "1" value={this.props.level}/></li>  
      <li>Gender: <button type="button" className={femaleString} width = {{width:"20%"}}>Female</button>
<button type="button" className={maleString} width = {{width:"20%"}}> Male</button></li>  
    </ul>
    
    
    </p> </div>
</div> 
   </div>
   )
      
 } 
  
  
  
});

var GroupingElement = new React.createClass({
  
render: function(){
  
var groupList = this.props.groups.map(function(g,n){
  
 return (
 <div className = "col-sm-4">  
   <GroupElement names = {g.names} index = {n+1}/>
 </div>
 ) 
  
  
})
 
return (<div className = "row">
  
  {groupList}
  
  
  </div>)  
  
  
}  
  
  
  
})

var GroupElement = React.createClass({
  
render:function(){

  
  var nameElements = this.props.names.map(function(s){
    
    return (<li className="list-group-item"><h3>{s}</h3></li>)
    
  })
  
return (

<div className="card" style = {{margin:"10px"}}>
 
  <div className="card-block card-header">
    <h4 className="card-title ">Group #{this.props.index}</h4>
    
  </div>
  <ul className="list-group list-group-flush">
    
    {nameElements}
    
  </ul>
  
</div>


)  
  
  
}  
  
  
})
      
      

  ReactDOM.render(<App names = {studentNames}/>,document.getElementById("main"))
  
  
            
          
!
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