เราต้องเขียนฟังก์ชันที่รับสตริงและคืนค่าสตริงใหม่ที่คำทั้งหมดของสตริงเดิมถูกกลับลำดับ
ตัวอย่าง: หากสตริงคือ:
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