Viết hàm nhận vào chuỗi: nếu ký tự đầu là chữ hoa → chuyển toàn bộ thành chữ hoa; ngược lại chuyển thành chữ thường.
Ví dụ
Mã thực hiện:
const str1 = "This is a normal string";
const str2 = "thisIsACamelCasedString";
const changeStringCase = str => {
let newStr = '';
const isUpperCase = str[0].charCodeAt(0) >= 65 && str[0].charCodeAt(0) <= 90;
if(isUpperCase){
newStr = str.toUpperCase();
}else{
newStr = str.toLowerCase();
};
return newStr;
};
console.log(changeStringCase(str1));
console.log(changeStringCase(str2));
Kết quả
Kết quả in ra console:
THIS IS A NORMAL STRING thisisacamelcasedstring