sayHello.js (8kyu 99)
Codewars 알고리즘 풀이
Problem
- Create a method sayHello that takes as input a ‘name’, ‘city’, and ‘state’ to welcome a person.
- ‘name’, ‘city’, ‘state’를 input으로 하는 메소드를 작성한다.
- Note that ‘name’ will be an array consisting of one or more values that should be joined together with one space between each, and the length of the ‘name’ array in test cases will vary.
- ‘name’은 하나 이상의 값으로 구성된 배열이고, 각각 공백으로 결합되어야 한다.
For example
sayHello(['Sam', 'Azor'], 'Dubbo', 'New South Wales');
// Hello, Sam Azor! Welcome to Dubbo, New South Wales!
Solution 01
function sayHello(name, city, state) {
return 'Hello, ' + name.join(' ') + '! Welcome to ' + city + ', ' + state + '!';
}
sayHello(['Sam', 'Azor'], 'Dubbo', 'New South Wales'); // Hello, Sam Azor! Welcome to Dubbo, New South Wales!
sayHello(['Ralph', 'Donovan'], 'Seoul', 'South Korea'); // Hello, Ralph Donovan! Welcome to Seoul, South Korea!
join()
메소드배열의 모든 element를 결합하고, 새 문자열로 반환한다.
Solution 02
function sayHello(name, city, state) {
return `Hello, ${name.join(' ')}! Welcome to ${city}, ${state}!`;
}
sayHello(['Sam', 'Azor'], 'Dubbo', 'New South Wales'); // Hello, Sam Azor! Welcome to Dubbo, New South Wales!
sayHello(['Ralph', 'Donovan'], 'Seoul', 'South Korea'); // Hello, Ralph Donovan! Welcome to Seoul, South Korea!