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.
Similar Kata:
Stats:
Created | Dec 17, 2016 |
Published | Dec 17, 2016 |
Warriors Trained | 103 |
Total Skips | 1 |
Total Code Submissions | 285 |
Total Times Completed | 43 |
JavaScript Completions | 43 |
Total Stars | 3 |
% of votes with a positive feedback rating | 92% of 24 |
Total "Very Satisfied" Votes | 21 |
Total "Somewhat Satisfied" Votes | 2 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 6 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 6 kyu |