Skip to content

Instantly share code, notes, and snippets.

@hpneo
Last active August 11, 2019 02:54
Show Gist options
  • Save hpneo/e36bc259cd864707a82c19c2977d2293 to your computer and use it in GitHub Desktop.
Save hpneo/e36bc259cd864707a82c19c2977d2293 to your computer and use it in GitHub Desktop.

From: https://www.codewars.com/kata/whos-online

You have a group chat application, but who is online!?

You want to show your users which of their friends are online and available to chat!

Given an input of an array of objects containing usernames, status and time since last activity (in mins), create a function to work out who is online, offline and away.

If someone is online but their lastActivity was more than 10 minutes ago they are to be considered away.

The input data has the following structure:

[{
  username: 'David',
  status: 'online',
  lastActivity: 10
}, {
  username: 'Lucy', 
  status: 'offline',
  lastActivity: 22
}, {
  username: 'Bob', 
  status: 'online',
  lastActivity: 104
}]

The corresponding output should look as follows:

{
  online: ['David'],
  offline: ['Lucy'],
  away: ['Bob']
}

If for example, no users are online the output should look as follows:

{
  offline: ['Lucy'],
  away: ['Bob']
}

username will always be a string, status will always be either 'online' or 'offline' and lastActivity will always be number >= 0.

Finally, if you have no friends in your chat application, the input will be an empty array []. In this case you should return an empty object {}.

const whosOnline = (friends) => {
// Your code here...
}
def who_is_online(friends)
return
end
describe('Given examples', () => {
var friends
test('Example test one of each', () => {
friends = [{
username: 'David',
status: 'online',
lastActivity: 10
}, {
username: 'Lucy',
status: 'offline',
lastActivity: 22
}, {
username: 'Bob',
status: 'online',
lastActivity: 104
}]
expect(whosOnline(friends)).toEqual({
online: ['David'],
offline: ['Lucy'],
away: ['Bob']
})
})
test('Example test no one online', () => {
friends = [{
username: 'Lucy',
status: 'offline',
lastActivity: 22
}, {
username: 'Bob',
status: 'online',
lastActivity: 104
}]
expect(whosOnline(friends)).toEqual({
offline: ['Lucy'],
away: ['Bob']
})
})
})
friends = [{"username"=> "David", "status"=> "online", "last_activity"=> 10},
{"username"=> "Lucy", "status"=> "offline", "last_activity"=> 22},
{"username"=> "Bob", "status"=> "online", "last_activity"=> 104}]
Test.assert_equals(who_is_online(friends), {"online"=> ["David"], "offline"=> ["Lucy"], "away"=> ["Bob"]})
friends = [{"username"=> "Lucy", "status"=> "offline", "last_activity"=> 22},
{"username"=> "Bob", "status"=> "online", "last_activity"=> 104}]
Test.assert_equals(who_is_online(friends), {"offline"=> ["Lucy"], "away"=> ["Bob"]})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment