Find numeral in string Flashcards

1
Q

Step 1: Set up Question

A

const str = “is2 Thi1s T4est 3a”;
const sortByNumber = (str = ‘’) => {
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Step 2: Find by number

A

const findNumber = (s = ‘’) => s
.split(‘’)
.reduce((acc, val) => +val ? +val : acc, 0);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Step 3: Make an array

A

const arr = str.split(‘ ‘);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Step 4: Sorter Function

A

const sorter(a,b) => {
return findNumber(a) - findNumber(b);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Step 5: sort the array

A

arr.sort(sorter);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Step 6: Join everything back together

A

return arr.join(‘ ‘);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Alternate Solution

A

function order(words){

return words.split(‘ ‘).sort(function(a, b){
return a.match(/\d/) - b.match(/\d/);
}).join(‘ ‘);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly