เราต้องเขียนฟังก์ชันที่รับสตริงตัวพิมพ์เล็กและเรียงในลำดับย้อนกลับ คือ b มาก่อน a, c มาก่อน b, เป็นต้น
ตัวอย่าง:
หากสตริงนำเข้าเป็น:
const str = "hello";
ผลลัพธ์ควรเป็น:
const output = "ollhe";
โค้ดสำหรับนี้:
const string = 'hello'; const sorter = (a, b) => { const legend = [-1, 0, 1]; return legend[+(a < b)]; } const reverseSort = str => { const strArr = str.split(''); return strArr .sort(sorter) .join(''); }; console.log(reverseSort(string));ผลลัพธ์ในคอนโซล:
ollhe