Looking for assistance with creating a function to sort a string where each word contains a single number from 1 to 9 (no 0).
For instance, given the input: "is2 Thi1s T4est 3a", the desired output should be "Thi1s is2 3a T4est".
The current code snippet:
function order(words)
{
// ...
if(words === '')
{
return words;
}
var all_words = words.split(" ");
var checked_words = new Array();
var joined_words = "";
for(i = 1; i <= 9; i++)
{
//console.log(checked_words);
//checked_words[i-1] = all_words;
for(j = 1;j <= all_words.length; j++)
{
if(all_words[i-1].indexOf(i) !== -1)
{
checked_words.push(all_words[i-1]);
if(i == (all_words.length))
{
joined_words = checked_words.join(" ");
return joined_words;
}
}
}
}
}
Encountering an issue of "TypeError: Cannot call method 'indexOf' of undefined at order." Any help would be greatly appreciated. Thank you!