-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhoLikesit?.js
22 lines (21 loc) · 1022 Bytes
/
whoLikesit?.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Implement the function which takes an array containing the names of people that like an item. It must return the display text as shown in the examples:
// [] --> "no one likes this"
// ["Peter"] --> "Peter likes this"
// ["Jacob", "Alex"] --> "Jacob and Alex like this"
// ["Max", "John", "Mark"] --> "Max, John and Mark like this"
// ["Alex", "Jacob", "Mark", "Max"] --> "Alex, Jacob and 2 others like this"
likes = names => {
let response = '';
if (names.length === 0) {
response += 'no one likes this';
} else if (names.length === 1) {
response += `${names[0]} likes this`;
} else if (names.length === 2) {
response += `${names[0]} and ${names[1]} like this`;
} else if (names.length === 3) {
response += `${names[0]}, ${names[1]} and ${names[2]} like this`;
} else if (names.length > 3) {
response += `${names[0]}, ${names[1]} and ${names.length - 2} others like this`;
}
return response;
}