Created
July 19, 2022 19:24
-
-
Save DonHuskini/ba648d7285ed39fc95e97f127c36c04d to your computer and use it in GitHub Desktop.
User following/followers
This file contains hidden or 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 { | |
id = undefined; | |
username = undefined; | |
following = []; | |
followers = []; | |
constructor({ id, username }) { | |
this.id = id; | |
this.username = username; | |
} | |
follow(user) { | |
if (this === user) throw Error("Cannot follow yourself"); | |
this.following.push(user); | |
user.followers.push(this); | |
} | |
get following() { | |
return this.following; | |
} | |
get followers() { | |
return this.followers; | |
} | |
} | |
const user1 = new User({ id: 1, username: "foo" }); | |
const user2 = new User({ id: 3, username: "bar" }); | |
const user3 = new User({ id: 4, username: "doo" }); | |
const user4 = new User({ id: 5, username: "wee" }); | |
const user5 = new User({ id: 6, username: "buz" }); | |
user1.follow(user2); | |
user1.follow(user4); | |
user1.follow(user5); | |
user2.follow(user1); | |
user3.follow(user2); | |
user3.follow(user5); | |
user5.follow(user2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment