Skip to content

Instantly share code, notes, and snippets.

@alex-hladun
Created May 27, 2020 23:06
Show Gist options
  • Save alex-hladun/3ccaa0377cc46408d1bdc2ad5a69444c to your computer and use it in GitHub Desktop.
Save alex-hladun/3ccaa0377cc46408d1bdc2ad5a69444c to your computer and use it in GitHub Desktop.
Given an object of social media metrics, returns certain stats.
const biggestFollower = function (data) {
let bigFollow = { name: "", followCount: 0 }
for (let id in data) {
if (data[id]['follows'].length > bigFollow.followCount) {
bigFollow.name = data[id]['name'];
bigFollow.followCount = data[id]['follows'].length;
}
}
console.log(`${bigFollow.name} follows the most people (${bigFollow.followCount}).`);
return bigFollow.name;
};
const mostPopular = function (data) {
let followerCount = {};
// Create list of how many followers each ID has.
for (let id in data) {
for (let i = 0; i <= data[id]['follows'].length - 1; i++) {
if (!followerCount[data[id]['follows'][i]]) {
followerCount[data[id]['follows'][i]] = 1;
} else {
followerCount[data[id]['follows'][i]] += 1;
}
}
}
// Find the number of the most amount of followers
let mostFollows = 0;
for (let id in followerCount) {
// console.log(followerCount[id])
if (followerCount[id] > mostFollows) {
mostFollows = followerCount[id];
}
}
//Create object for all users with that amount of followers (some are tied)
const mostPopularUsers = {};
for (let id in followerCount) {
if (followerCount[id] === mostFollows) {
for (let userID in data) {
if (id === userID) {
mostPopularUsers[data[userID]['name']] = mostFollows;
}
}
}
}
console.log("User(s) with the most followers:");
console.log(mostPopularUsers);
return mostPopularUsers;
};
const nameIDDatabase = function (data) {
// Create an object with ID/Name pairing.
const database = {};
for (let id in data) {
database[id] = data[id]['name'];
}
return database;
};
const unrequitedFol = function (data) {
let unReqList = [];
// will print ["Avery follows Alex, but he doesn't follow back", "Alex follows..."]
let followData = printAll(data);
for (let user in followData) {
for (let i = 0; i <= followData[user]['Following'].length - 1; i++) {
let flag = 0;
for (let j = 0; j <= followData[user]['Followers'].length - 1; j++) {
if (followData[user]['Following'][i] === followData[user]['Followers'][j]) {
flag += 1;
}
}
if (flag === 0) {
unReqList.push(`${user} follows ${followData[user]['Following'][i]} but they don't follow back!`);
}
}
}
console.log(unReqList);
return unReqList;
};
const printAll = function (data) {
const followData = {};
const database = nameIDDatabase(data);
// initialize followData array with names, and array for names of followers and following
for (let id in data) {
followData[data[id]['name']] = { Following: [], Followers: [] };
}
// Loop through each ID and populate following/follower arrays.
for (let id in data) {
for (let i = 0; i <= data[id]['follows'].length - 1; i++) {
let followingID = data[id]['follows'][i];
let followingName = database[followingID];
followData[data[id]['name']]['Following'].push(followingName);
followData[followingName]['Followers'].push(data[id]['name']);
}
}
// console.log(followData);
return followData;
};
const followerOver30 = function (data) {
// Who has most followers over 30
let database = nameIDDatabase(data);
// create list of ID's with followers over 30
let followOver30Obj = {};
for (let id in data) {
if (data[id].age > 30) {
for (let i = 0; i <= data[id]['follows'].length - 1; i++) {
if (followOver30Obj[data[id]['follows'][i]]) {
followOver30Obj[data[id]['follows'][i]] += 1;
} else {
followOver30Obj[data[id]['follows'][i]] = 1;
}
}
}
}
// find ID with most followers
// convert ID to name
let over30Count = 0;
let name = "";
for (let id in followOver30Obj) {
if (followOver30Obj[id] > over30Count) {
over30Count = followOver30Obj[id];
name = database[id];
}
}
console.log(`${name} has the most followers over 30 (${over30Count})`);
return name;
};
const followOver30 = function (data) {
let database = nameIDDatabase(data);
let idListOver30 = [];
for (let id in data) {
if (data[id].age > 30) {
idListOver30.push(id);
}
}
let followingOver30Obj = {};
for (let id in data) {
for (let x of data[id].follows) {
for (let y of idListOver30) {
if (x === y) {
if (followingOver30Obj[id]) {
followingOver30Obj[id] += 1;
} else {
followingOver30Obj[id] = 1;
}
}
}
}
}
let followCount = 0;
let name = "";
for (let id in followingOver30Obj) {
if (followingOver30Obj[id] > followCount) {
followCount = followingOver30Obj[id];
name = database[id];
}
}
console.log(`${name} follows the most people over 30 (${followCount})`);
return name;
};
const userReach = function (data) {
let followData = printAll(data);
// console.log(followData);
let database = nameIDDatabase(data);
let reach = {};
// create list of each persons followers
for (let name in followData) {
reach[name] = followData[name]['Followers'];
};
// add followers of followers
for (let name in reach) {
for (let follower of reach[name]) {
reach[name] = reach[name].concat(followData[follower]['Followers']);
}
}
// remove duplicates and same name
for (let name in reach) {
let newArray = reach[name].filter((item, index) => reach[name].indexOf(item) === index);
reach[name] = newArray.filter(person => person !== name);
}
let reachCount = {};
for (let name in reach) {
reachCount[name] = reach[name].length;
}
// console.log(reach);
console.log("Reach count");
console.log(reachCount);
};
const data = {
f01: {
name: "Alice",
age: 15,
follows: ["f02", "f03", "f04", "f07", "f05"]
},
f02: {
name: "Bob",
age: 20,
follows: ["f05", "f06", "f07"]
},
f03: {
name: "Charlie",
age: 35,
follows: ["f01", "f04", "f06"]
},
f04: {
name: "Debbie",
age: 40,
follows: ["f01", "f02", "f03", "f05", "f06", "f07"]
},
f05: {
name: "Elizabeth",
age: 45,
follows: ["f04", "f07"]
},
f06: {
name: "Finn",
age: 25,
follows: ["f05", "f07"]
},
f07: {
name: "Alex",
age: 27,
follows: []
}
};
biggestFollower(data);
mostPopular(data);
printAll(data);
nameIDDatabase(data);
unrequitedFol(data);
followerOver30(data);
followOver30(data);
userReach(data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment