-
Notifications
You must be signed in to change notification settings - Fork 3
/
Your_order_please.js
44 lines (32 loc) · 1.12 KB
/
Your_order_please.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Kata link:
// https://www.codewars.com/kata/55c45be3b2079eccff00010f/
//
// -------------------------------------
// Instructions:
/*
Your task is to sort a given string. Each word in the string will contain
a single number. This number is the position the word should have in the result.
Note: Numbers can be from 1 to 9. So 1 will be the first word (not 0).
If the input string is empty, return an empty string. The words in the input
String will only contain valid consecutive numbers.
Examples
"is2 Thi1s T4est 3a" --> "Thi1s is2 3a T4est"
"4of Fo1r pe6ople g3ood th5e the2" --> "Fo1r the2 g3ood 4of th5e pe6ople"
"" --> ""
*/
// -------------------------------------
// Solution:
function order(words) {
let obj = {};
for (let i of words.split(' ')) {
let tp = i.search(/\d/g);
obj[i[tp]] = i;
}
return Object.values(obj).join(' ');
}
// -------------------------------------
// Basic Tests
console.log(order("Tests:"))
console.log(order("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("") == "")