I've been struggling with a coding challenge for a couple of hours now and could really use some help. Here are the detailed instructions:
Take an array of strings and create a single string by following these steps:
Repeat the following steps as long as there is more than one string in the array:
Identify the shortest string in the array (if there are multiple strings of the same length, choose the leftmost one);
Find the shortest string among the remaining strings (if there are multiple strings of the same length, choose the rightmost one);
Remove the selected strings from the array;
Append the concatenated result of the chosen strings (the second string should be added to the end of the first string) to the right end of the array.
Once the algorithm is complete, there should be only one string left in the array. Return that string.
Here's my attempt at a solution:
function concatenationProcess(init) {
var shortestString = init[0];
var shorterString = init[0];
var appendedString = "";
while (init.length > 0) {
for (i = init.length - 1; i >= 0; i--) {
if (init[i].length <= shortestString.length) {
shortestString = init[i];
}
}
var newArray = init;
newArray = init.splice(init.indexOf(shortestString), 1)
for (i = 0; i < init.length; i++) {
if (init[i].length <= shorterString.length) {
shorterString = init[i];
}
}
init.splice(init.indexOf(shorterString), 1)
var newInit = init;
console.log(shorterString, "shorter string")
appendedString = shortestString + shorterString
init.push(appendedString)
console.log(init)
}
}