Find numeral in string Flashcards
1
Q
Step 1: Set up Question
A
const str = “is2 Thi1s T4est 3a”;
const sortByNumber = (str = ‘’) => {
}
2
Q
Step 2: Find by number
A
const findNumber = (s = ‘’) => s
.split(‘’)
.reduce((acc, val) => +val ? +val : acc, 0);
3
Q
Step 3: Make an array
A
const arr = str.split(‘ ‘);
4
Q
Step 4: Sorter Function
A
const sorter(a,b) => {
return findNumber(a) - findNumber(b);
}
5
Q
Step 5: sort the array
A
arr.sort(sorter);
6
Q
Step 6: Join everything back together
A
return arr.join(‘ ‘);
7
Q
Alternate Solution
A
function order(words){
return words.split(‘ ‘).sort(function(a, b){
return a.match(/\d/) - b.match(/\d/);
}).join(‘ ‘);
}