So I am doing a an exercise in which I have to sort a given string. Each word in the string contains a number in it(written like this 'H3llo'). The number that is in each word of the string should be placed in order according to the number in the new string that is to be returned.
For example if my input is "is2 Thi1s T4est 3a", then my function should return "Thi1s is2 3a T4est".
I almost cracked it but my output is incomplete. Here is my code:
function order(words) {
var lst = words.split(' ');
var count = 0;
var n = count + 1;
var s_n = n.toString();
var new_l = [];
while (count < lst.length) {
if (lst[count].includes(s_n) === true) {
new_l.push(lst[count])
}
count++
}
return new_l.join(' ');
}
When I test it, instead of getting:
console.log(order("is2 Thi1s T4est 3a"));
>>> 'Thi1s is2 3a T4est'
I get this:
console.log(order("is2 Thi1s T4est 3a"));
>>> 'Thi1s'
Can anyone explain to me what I am doing wrong?
Array#sort.nands_nneed to be set inside the loop.;aftercount++andnew_l.push(lst[count])and do what @Malk told you increases_ninside loop