https://www.codewars.com/kata/5266876b8f4bf2da9b000362
Codewars - Achieve mastery through coding practice and developer mentorship
Coding practice for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!
www.codewars.com
DESCRIPTION:
You probably know the "like" system from Facebook and other pages. People can "like" blog posts, pictures or other items. We want to create the text that should be displayed next to such an item.
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"
Note: For 4 or more names, the number in "and 2 others" simply increases.
설명:
당신은 아마 페이스북이나 다른 페이지의 "좋아요" 시스템을 알고 있을 것입니다. 사람들은 블로그 포스트, 사진, 혹은 다른 항목들을 "좋아요" 할 수 있습니다. 우리는 그러한 항목 옆에 표시되어야 하는 문구를 만들려고 합니다.
항목을 좋아한 사람의 이름을 포함한 배열을 가지는 함수를 구현하세요. 다음 예와 같은 표시 문구를 반환해야 합니다.
[] --> "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"
Note: 이름이 4명 이상인 경우 간단히 "and 2 others"의 숫자가 증가합니다.
풀이
function likes(names) {
if(names.length === 0) return "no one likes this";
else if(names.length === 1) return names[0] + " likes this";
else if(names.length === 2) return names[0] +" and "+ names[1] + " like this";
else if(names.length === 3) return names[0] +", "+ names[1] + " and " + names[2] + " like this";
else return names[0] + ", " + names[1] + " and " + (names.length - 2) + " others like this";
}
names 배열의 길이에 따라 조건을 따져서 문장을 만들어 반환한다.
'알고리즘 > Codewars' 카테고리의 다른 글
[Codewars] Convert a Number to a String! (8 kyu) / JavaScript (0) | 2022.10.10 |
---|---|
[Codewars] Convert a String to a Number! (8 kyu) / JavaScript (0) | 2022.10.10 |
[Codewars] Convert number to reversed array of digits (8 kyu) / JavaScript (0) | 2022.10.10 |
[Codewars] Vowel Count (7 kyu) / JavaScript (0) | 2022.10.10 |
[Codewars] Array.diff (6 kyu) / JavaScript (0) | 2022.10.09 |
댓글