เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่ใช้สตริงตัวพิมพ์เล็กและเรียงลำดับกลับกัน เช่น 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