ปัญหา
เราต้องเขียนฟังก์ชัน JavaScript ที่รับสตริงและคืนค่าสตริงใหม่ที่ไม่มีช่องว่าง (สตริงที่ช่องว่างทั้งหมดถูกแทนที่ด้วยสตริงว่าง)
ตัวอย่าง
โค้ดตัวอย่างดังนี้:
const str = 'some random string ex a m pl e';
const removeSpaces = (str = '') => {
let res = '';
for(let i = 0; i < str.length; i++){
const el = str[i];
if(el !== ' '){
res += el;
continue;
};
};
return res;
};
console.log(removeSpaces(str));
ผลลัพธ์
somerandomstringexample