Created
May 19, 2024 03:59
-
-
Save tanvirstreame/569e8adac39d9c3e60491b5b2dda21a9 to your computer and use it in GitHub Desktop.
Twiller (Keep list / map outside of class)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User { | |
constructor(userId, fullName, phone, age) { | |
this.userId = userId; | |
this.fullName = fullName; | |
this.phone = phone; | |
this.age = age; | |
} | |
} | |
class Follow { | |
constructor(followee, follower) { | |
this.followee = followee; | |
this.follower = follower; | |
} | |
} | |
class Tweet { | |
constructor(userId, post) { | |
this.userId = userId; | |
this.post = post; | |
} | |
} | |
const users = []; | |
const follows = []; | |
const tweets = []; | |
// Register users | |
const user1 = new User('u1', 'John Doe', '123-456-7890', 30); | |
const user2 = new User('u2', 'Jane Smith', '987-654-3210', 25); | |
users.push(user1, user2); | |
// Add follow relationships | |
const follow1 = new Follow('u2', 'u1'); | |
follows.push(follow1); | |
// Add tweets | |
const tweet1 = new Tweet('u1', 'Hello world!'); | |
const tweet2 = new Tweet('u2', 'Good morning!'); | |
tweets.push(tweet1, tweet2); | |
// Get followee tweets for a user | |
function getFolloweeTweets(userId) { | |
const followeeList = follows | |
.filter(follow => follow.follower === userId) | |
.map(follow => follow.followee); | |
return tweets.filter(tweet => followeeList.includes(tweet.userId)); | |
} | |
const followeeTweets = getFolloweeTweets('u1'); | |
console.log(followeeTweets); // Should show Jane's tweet |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment