Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function comments(input) {
- let usersList = [];
- let articlesList = [];
- let output = {}
- for (let command of input) {
- if (command.substr(0, 4) === 'user') {
- let newUser = command.split(' ')[1];
- usersList.push(newUser);
- } else if (command.substr(0, 7) === 'article') {
- let newArticle = command.split(' ')[1];
- articlesList.push(newArticle);
- } else {
- let [userAndArticleName, commentTitleAndContent] = command.split(': ');
- let [userName, articleName] = userAndArticleName.split(' posts on ');
- let [commentTitle, commentContent] = commentTitleAndContent.split(', ');
- if (usersList.includes(userName) && articlesList.includes(articleName)) {
- if (!Object.keys(output).includes(articleName)) {
- output[articleName] = [];
- }
- output[articleName].push([commentTitle, userName, commentContent])
- }
- }
- }
- output = Object.entries(output);
- output.sort((a, b) => b[1].length / 2 - a[1].length / 2);
- output.forEach(element => {
- element[1].sort((a, b) => a[1].localeCompare(b[1]));
- });
- output.forEach(element => {
- console.log(`Comments on ${element[0]}`);
- element[1].forEach(element => {
- console.log(`--- From user ${element[1]}: ${element[0]} - ${element[2]}`);
- });
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment