6 kyu

Easy Peer-to-Peer Telephone Game

Description:

As per Christmas tradition, your friends have been playing the telephone game all night!

Now you want to get the real starting message but each of your friends only has a piece of the message.

Your task is to get the full message given the limited memory capacity of each of your friends.

Each of your peers will be an object with a method getData. You pass in a key for the message you want and they will tell you what they know.

function Peer() {
  let data = {};

  this.addData = function (id, startIdx, piece) {
    if (!data[id]) {
      data[id] = [];
    }
    data[id].push({start:startIdx, data:piece});
  };

  this.getData = function (id) {
    // make a copy for clean housekeeping
    return data[id] ? data[id].concat([]) : false;
  };
}

Each peer will know at what index of the message their piece starts and the text they remember. They may remember several pieces. A peer will return an array of objects containg the start and data of the pieces of the message they remember. If the peer has no data for the message, they will return false.

If a peer returns: [{start: 0, data: `hello `}, {start: 6, data: 'world'}] you would know the full message is 'hello world'.

Among the peers you will always get the full message.

Your job is to complete the peerToPeer method that takes as parameters an array of all your peers and the key id of the message. Gather what your peers know and reconstruct the message.

Algorithms

More By Author:

Check out these other kata created by Austin Haws

Stats:

CreatedDec 17, 2016
PublishedDec 17, 2016
Warriors Trained103
Total Skips1
Total Code Submissions285
Total Times Completed43
JavaScript Completions43
Total Stars3
% of votes with a positive feedback rating92% of 24
Total "Very Satisfied" Votes21
Total "Somewhat Satisfied" Votes2
Total "Not Satisfied" Votes1
Total Rank Assessments6
Average Assessed Rank
5 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • Austin Haws Avatar
  • smile67 Avatar
Ad