ปัญหา
เราจำเป็นต้องเขียนฟังก์ชัน 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