ปัญหา
เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่รับสตริง camelCase โดย str เป็นอาร์กิวเมนต์แรกและอาร์กิวเมนต์เดียว
ฟังก์ชันของเราควรสร้างและส่งคืนสตริงใหม่ที่แยกสตริงอินพุตโดยใช้ช่องว่างระหว่างคำ
ตัวอย่างเช่น หากอินพุตของฟังก์ชันคือ −
ป้อนข้อมูล
const str = 'thisIsACamelCasedString';
ผลผลิต
const output = 'this Is A Camel Cased String';
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
const str = 'thisIsACamelCasedString';
const breakCamelCase = (str = '') => {
const isUpper = (char = '') => char.toLowerCase() !==
char.toUpperCase() && char === char.toUpperCase();
let res = '';
const { length: len } = str;
for(let i = 0; i < len; i++){
const el = str[i];
if(isUpper(el) && i !== 0){
res += ` ${el}`;
continue;
};
res += el;
};
return res;
};
console.log(breakCamelCase(str)); ผลลัพธ์
this Is A Camel Cased String