I need to make a function that will sort a string. Each word in the String will contain a single number. Numbers can be from 1 to 9(no 0).
For example an input: "is2 Thi1s T4est 3a" the function should return "Thi1s is2 3a T4est".
My code is :
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;
}
}
}
}
}
The problem is it kept showing "TypeError: Cannot call method 'indexOf' of undefined at order". Please help thanks!