All files GossipExchangeStation.js

100% Statements 26/26
100% Branches 4/4
100% Functions 12/12
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56    14x 14x 14x       14x 14x 14x 14x       17x 39x 129x 75x         17x 39x         16x 36x         13x 13x   13x 13x 13x 40x   13x   13x 8x 18x     13x          
class GossipExchangeStation {
  constructor(stationProperties) {
    this.numberOfGossips = stationProperties.numberOfGossips;
    this.busdriversAtStation = stationProperties.busdriversAtStation;
    this.initialize();
  }
 
  initialize() {
    this.stationGossipArray = new Array(this.numberOfGossips);
    this.stationGossipArray.fill(false);
    this.collectGossips();
    this.updateDriverGossips();
  }
 
  collectGossips() {
    const passGossips = (driverGossipArray) => {
      driverGossipArray.forEach((gossip, index) => {
        if (gossip) {
          this.stationGossipArray[index] = true;
        }
      });
    };
 
    this.busdriversAtStation.forEach((driver) => {
      passGossips(driver.gossipArray);
    });
  }
 
  updateDriverGossips() {
    this.busdriversAtStation.forEach((driver) => {
      driver.gossipArray = this.stationGossipArray;
    });
  }
 
  getDriversKnowAllGossips() {
    let driversWithFullKnowledge = new Array(this.numberOfGossips);
    driversWithFullKnowledge.fill(false);
 
    const areAllGossipsKnownAtStation = () => {
      let result = 1;
      this.stationGossipArray.forEach((gossip) => {
        result *= gossip;
      });
      return result === 1;
    };
    if (areAllGossipsKnownAtStation()) {
      this.busdriversAtStation.forEach((driver) => {
        driversWithFullKnowledge[driver.id] = true;
      });
    }
    return driversWithFullKnowledge;
  }
}
 
export default GossipExchangeStation;