I'm trying to complete a codewars exercise in which you simply return a string of words in order based on the number in the string.
example:
order("is2 Thi1s T4est 3a") // should return "Thi1s is2 3a T4est"
order("4of Fo1r pe6ople g3ood th5e the2") // should return "Fo1r the2 g3ood 4of th5e pe6ople")
This is my attempt so far:
function order(words) {
let wordsArr = words.split(' ')
let result = [];
for (let i = 0; i < wordsArr.length; i++) {
for (let j = 0; j < wordsArr[i].length; j++) {
if (typeof wordsArr[i][j] === 'number') {
result[wordsArr[i][j]] = wordsArr[i]
}
}
}
return result
}
However this just returns an empty array. My logic is that I am looping through each letter of each word in wordsArr, and once the typeof letter matches 'number', then I set the results array index of wordsArr[i][j] equal to wordsArr[i]. This isn't working the way I'm expecting it to though, and I'm puzzled as to why!
wordsArris a string, so the result of thesplit()will never have items that arenumbertype.wordsArr[i][j]inNumber(), this should convert the singular string letter into a number if applicable