본문 바로가기
알고리즘/Codewars

[Codewars] Hello, Name or World! (8 kyu) / JavaScript

by fluss 2023. 3. 13.

https://www.codewars.com/kata/57e3f79c9cb119374600046b

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website 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:

Define a method hello that returns "Hello, Name!" to a given name, or says Hello, World! if name is not given (or passed as an empty String).

Assuming that name is a String and it checks for user typos to return a name with a first capital letter (Xxxx).

Examples:

* With `name` = "john"  => return "Hello, John!"
* With `name` = "aliCE" => return "Hello, Alice!"
* With `name` not given 
  or `name` = ""        => return "Hello, World!"

 

코드

function hello(name) {
  if(!name) return "Hello, World!";
  name = name[0].toUpperCase() + name.slice(1).toLowerCase();
  return `Hello, ${name}!`;
}

댓글