Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tanvirstreame/4f682ead91d03c39c697a56234677ba1 to your computer and use it in GitHub Desktop.
Save tanvirstreame/4f682ead91d03c39c697a56234677ba1 to your computer and use it in GitHub Desktop.
Facebook System ( list/ map outside of the class)
class User {
constructor(userId, userName) {
this.userId = userId;
this.userName = userName;
}
}
class Post {
constructor(userId, posts) {
this.userId = userId;
this.posts = posts;
}
}
class Friend {
constructor(follower, followee) {
this.follower = follower;
this.followee = followee;
}
}
const users = new Map();
const friends = [];
const posts = [];
function registerUser(userId, userName) {
if (!users.has(userId)) {
const newUser = new User(userId, userName);
users.set(userId, newUser);
return newUser;
}
return "User already existed";
}
function addFriend(userId, friendId) {
friends.push(new Friend(userId, friendId));
friends.push(new Friend(friendId, userId));
return "Added as friend";
}
function removeFriend(userId, friendId) {
friends = friends.filter(friend => !(friend.followee === userId && friend.follower === friendId));
friends = friends.filter(friend => !(friend.followee === friendId && friend.follower === userId));
return "Unfriended successfully";
}
function addPost(userId, postContent) {
posts.push(new Post(userId, postContent));
return "Added post successfully";
}
function getFriendPosts(userId) {
const friendList = friends.filter(eachUser => eachUser.follower === userId).map(friend => friend.followee);
return posts.filter(post => friendList.includes(post.userId));
}
function searchFriendPosts(userId, search) {
const friendList = friends.filter(eachUser => eachUser.follower === userId).map(friend => friend.followee);
const postList = posts.filter(post => friendList.includes(post.userId));
return postList.filter(post => post.posts.toLowerCase().includes(search.toLowerCase()));
}
// Usage example:
registerUser(1, 'Tanvir');
registerUser(2, 'Nadim');
addFriend(1, 2);
addPost(1, 'Hello, friends!');
addPost(2, 'Hi, Bangladesh!');
console.log("friendPost", getFriendPosts(1));
console.log("friendPost search", searchFriendPosts(1, "bangladesh"));
removeFriend(1, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment