เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่ใช้สตริงที่มีตัวพิมพ์ใหญ่และตัวพิมพ์เล็ก ฟังก์ชันควรส่งคืนสตริงโดยย้ายตัวอักษรพิมพ์ใหญ่ทั้งหมดไปไว้ข้างหน้าสตริง
ตัวอย่างเช่น หากสตริงอินพุตคือ −
const str = 'heLLO woRlD';
จากนั้นผลลัพธ์ควรเป็น −
const output = 'LLORDhe wol';
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
const str = 'heLLO woRlD';
const moveCapitalToFront = (str = '') => {
let capitalIndex = 0;
const newStrArr = [];
for(let i = 0; i < str.length; i++){
if(str[i] !== str[i].toLowerCase()){
newStrArr.splice(capitalIndex, 0, str[i]);
capitalIndex++;
}else{
newStrArr.push(str[i]);
};
};
return newStrArr.join('');
};
console.log(moveCapitalToFront(str)); ผลลัพธ์
ต่อไปนี้เป็นผลลัพธ์ในคอนโซล -
LLORDhe wol