I'm trying to order strings in an array depending on the number present in them, ie. 'h2ello f3ere b1ow' should return an array of ['b1ow', 'h2ello' ,' f3ere']. The following code works with two elements (h2ello and b1ow), but not when I add a third. Does anyone have an idea why this is?
function order(words){
var sentence = [];
words = words.split(" ");
for (var i=0;i<words.length;i++){
for (var m=0;m<words[i].length;m++){
if (!isNaN(parseFloat(words[i][m])) && isFinite(words[i][m])){
var idx = words[i][m];
sentence.splice(idx, 0, words[i]);
}
}
}
console.log(sentence);
}
order('h2ello f3ere b1ow');