Who likes it? ( 6 kyu )

문제

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 a function likes :: [String] -> String, which must take in input array, containing the names of people who like an item. It must return the display text as shown in the examples:

likes [] // must be "no one likes this"
likes ["Peter"] // must be "Peter likes this"
likes ["Jacob", "Alex"] // must be "Jacob and Alex like this"
likes ["Max", "John", "Mark"] // must be "Max, John and Mark like this"
likes ["Alex", "Jacob", "Mark", "Max"] // must be "Alex, Jacob and 2 others like this"

배열의 사람 수에따라 다른 문자열을 출력하는 문제

풀이

function likes(names) {
  switch ( names.length ) {
    case 0: return 'no one likes this';
    case 1: return `${names[0]} likes this`;
    case 2: return `${names[0]} and ${names[1]} like this`;
    case 3: return `${names[0]}, ${names[1]} and ${names[2]} like this`;
    default: return `${names[0]}, ${names[1]} and ${names.length-2} others like this`;
  }
}

코드워즈를 풀면서 처음으로 스위치 케이스를 사용함.

어려운 문젠 아닌듯

다른 사람의 풀이

똑같이 풀어서 안가져옴


'자료구조, 알고리즘 > Codewars 문제' 카테고리의 다른 글

Tribonacci Sequence ( 6 kyu )  (0) 2017.11.28
Valid Braces ( 4 kyu )  (0) 2017.11.27
Unique In Order ( 6 kyu )  (0) 2017.11.26
Counting Duplicates ( 6 kyu )  (0) 2017.11.26
Vasya - Clerk ( 6 kyu )  (0) 2017.11.26

+ Recent posts