Edabit
Given a sentence with numbers representing a word's location embedded within each word, return the sorted sentence.
Examples
rearrange("is2 Thi1s T4est 3a") ➞ "This is a Test" rearrange("4of Fo1r pe6ople g3ood th5e the2") ➞ "For the good of the people" rearrange(" ") ➞ ""
Notes
Only the integers 1-9 will be used.
Solution:
1 2 3 4 5 6 7 8 9 | function rearrange(sentence) { let arr = sentence.trim().split(" "); arr.sort((a, b) => { return ( parseInt(a.replace(/[^0-9]/g, "")) - parseInt(b.replace(/[^0-9]/g, "")) ); }); return arr.map((a) => a.replace(/[0-9]/g, "")).join(" "); } |
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.