js-mentorship-razvan / javascript

Javascript study notes
GNU General Public License v3.0
22 stars 2 forks source link

Your order, please #597

Closed RazvanBugoi closed 2 years ago

RazvanBugoi commented 2 years ago

https://www.codewars.com/kata/55c45be3b2079eccff00010f/train/javascript

RazvanBugoi commented 2 years ago
function order(words){
  const arr = words.split(" ")

  arr.sort(function(a,b) {
    const numberA = Number(a.match(/\d/g)[0])
    const numberB = Number(b.match(/\d/g)[0])
    if (numberA < numberB) {
        return - 1;
    }
    if (numberA > numberB) {
        return 1;
    }

    return 0
})

  return words ? arr.join(" ") : ""
}