Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Viết hoa chữ cái đầu mỗi từ trong câu (Title Case) JavaScript

Yêu cầu: Viết hàm nhận một chuỗi và viết hoa chữ cái đầu của mỗi từ, chuyển các chữ cái còn lại thành chữ thường.

Ví dụ:

hello world coding is very interesting

Kết quả mong đợi:

Hello World Coding Is Very Interesting

Định nghĩa hàm capitaliseTitle():

Ví dụ

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));

Kết quả

Đầu ra console:

Hello World Coding Is Very Interesting