I am trying to unscramble an array. The array has string items out of order with a number attached to specify what the order should be. I want to take the number within the array item and reassign the array item index to reflect that number. For example var scrambled = ["pizza4", "to2", "I0", "eat3", "want1"]. I have a function that searches the array for the number attached and returns the value. Now I want to take the value returned and turn it into the new item's index in a new array, like var unscrambled = []. This is what I have so far:
function unscramblePhrase() {
var scrambled = ["pizza4", "to2", "I0", "eat3", "want1"];
var unscrambled = [];
for (var counter = 0; counter < scrambled.length; counter++) {
numPosition = scrambled[counter].search('[0-9]');
arrayIndex = scrambled[counter].substring(numPosition);
console.log(arrayIndex);
unscrambled.push(scrambled[arrayIndex]);
}
console.log(unscrambled)
}
I see that my arrayIndex is pulling the numbers from the end of the scrambled array items but my attempt at assigning index position based off this variable is producing a newly scrambled array: ["want1", "I0", "pizza4", "eat3", "to2"].
"abc10def22"? What if there are strings with the same number:["abc12", "def12"]? What if there is a missing number:["abc0", "def3"](2is missing)?