문자열을 받아들이고 해당 문자열에 있는 모든 단어의 첫 글자를 대문자로 바꾸고 나머지 모든 글자의 대소문자를 소문자로 바꾸는 함수를 작성해야 한다고 가정해 봅시다.
예를 들어, 입력 문자열이 -
인 경우hello world coding is very interesting
출력은 다음과 같아야 합니다. -
Hello World Coding Is Very Interesting
문자열을 받아 각 단어의 첫 글자를 대문자로 바꾸고 문자열을 반환하는 함수 capitaliseTitle()을 정의합시다 -
예시
let str = 'hello world coding is very interesting';
const capitaliseTitle = (str) => {
const string = str
.toLowerCase()
.split(" ")
.map(word => {
return word[0]
.toUpperCase() + word.substr(1, word.length);
})
.join(" ");
return string;
}
console.log(capitaliseTitle(str)); 출력
콘솔 출력은 -
Hello World Coding Is Very Interesting