เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่รับสตริง ฟังก์ชันควรส่งคืนสตริงใหม่ที่มีการกลับคำทั้งหมดของสตริงเดิม
ตัวอย่างเช่น ถ้าสตริงคือ −
const str = 'this is a sample string';
จากนั้นผลลัพธ์ควรเป็น −
const output = 'siht si a elpmas gnirts';
ตัวอย่าง
รหัสสำหรับสิ่งนี้จะเป็น −
const str = 'this is a sample string';
const reverseWords = str => {
let reversed = '';
reversed = str.split(" ")
.map(word => {
return word
.split("")
.reverse()
.join("");
})
.join(" ");
return reversed;
};
console.log(reverseWords(str)); ผลลัพธ์
เอาต์พุตในคอนโซล −
siht si a elpmas gnirts