cachaito
5/26/2015 - 7:11 AM

Improving functions

//very lame
function board(ship, crewMan) {  
  openDoor(ship, crewMan);
  goInside(ship, crewMan);
  closeDoor(ship, crewMan);
}

function boardMany(ship, wholeCrew) {  
  for (var i=0; i < wholeCrew.length; i++) {
    board(ship, wholeCrew[i]);
  }
}

//nice
function board(ship, wholeCrew) {  
  wholeCrew = [].concat(wholeCrew); //nawet gdy podamy pojedynczego członka, gładko zostanie on dodany do tablicy
  wholeCrew.forEach(function(crewMan) {
    openDoor(ship, crewMan);
    goInside(ship, crewMan);
    closeDoor(ship, crewMan);
  });
}