Your order, please
codewars 6 kyu
by trentHarlem
JavaScript
function order(words) {
let input = words.split(' ')
let digits = /[1-9]/g
return input.sort((item, nextItem) => item.match(digits) - nextItem.match(digits)).join(' ')
}
/* function order(words) {
let input = words.split(' ')
if (input == '') return ''
let output = new Array(input.length).fill('x')
let digits = /[1-9]/g
input.map((item, i, arr) => output.splice(item.match(digits) - 1, 1, item))
return output.join(' ')
} */
/* function order(words) {
return words.split(" ").sort((a, b) => +a.match(/\d/)[0] - +b.match(/\d/)[0]).join(" ");
} */
const orderArrow = words => words.split(" ").sort((a, b) => +a.match(/\d/) - +b.match(/\d/)).join(" ")
/* function order(words) {
let input = words.split(' ')
if(input=='') return""
// let output = [...input]
let output = new Array(input.length).fill('x')
let digits = /[1-9]/
for (let word of words) {
for (let char of word) {
if (char.match(digits)) {
output.splice(+char - 1, 1, word)
}
}
}
return output.join(' ')
} */
//console.log(+'t')//NaN
console.log(orderArrow("is2 Thi1s T4est 3a"), "Thi1s is2 3a T4est")
console.log(order("4of Fo1r pe6ople g3ood th5e the2"), "Fo1r the2 g3ood 4of th5e pe6ople")
console.log(order(""), "", "empty input should return empty string")