Viết hàm nhận chuỗi, xây dựng và trả về từ viết tắt (acronym) chỉ lấy chữ cái đầu của các từ bắt đầu bằng chữ hoa.
Ví dụ: 'Polar Satellite Launch Vehicle' → 'PSLV'.
Ví dụ
const str = 'Polar Satellite Launch Vehicle';
const buildAcronym = (str = '') => {
const strArr = str.split(' ');
let res = '';
strArr.forEach(el => {
const [char] = el;
if (char === char.toUpperCase() && char !== char.toLowerCase()) res += char;
});
return res;
};
console.log(buildAcronym(str));
console.log(buildAcronym('Bachelor of Science'));
Kết quả
PSLV BS