Skip to content

Instantly share code, notes, and snippets.

@AngieCristina
Created July 9, 2019 14:43
Show Gist options
  • Save AngieCristina/0c11dbf66c1f27b80c25d12a7e367a42 to your computer and use it in GitHub Desktop.
Save AngieCristina/0c11dbf66c1f27b80c25d12a7e367a42 to your computer and use it in GitHub Desktop.
PairProgramming
const arr = [
{
username: "David",
status: "online",
lastActivity: 10
},
{
username: "Lucy",
status: "offline",
lastActivity: 22
},
{
username: "Bob",
status: "online",
lastActivity: 104
}
];
const whosOnline = function(arr) {
arr2 = arr.map(function(item) {
let newStatus;
if (item.status === "offline") {
newStatus = "offline";
} else if (item.status === "online" && item.lastActivity > 10) {
newStatus = "away";
} else {
newStatus = "online";
}
return { username: item.username, status: newStatus };
});
const online = [];
const offline = [];
const away = [];
const findResult = {};
arr2.forEach(element => {
if (element.status === "online") {
online.push(element.username);
} else if (element.status === "offline") {
offline.push(element.username);
} else if (element.status === "away") {
away.push(element.username);
}
});
if (online.length > 0) {
findResult["online"] = online;
}
if (offline.length > 0) {
findResult["offline"] = offline;
}
if (away.length > 0) {
findResult["away"] = away;
}
return findResult;
};
whosOnline(arr);
console.log(whosOnline(arr));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment