Trying to solve this kata on Codewars.
I've been able to reverse the array into a string, but haven't been able to assign this string into individual elements of a specified length. I tried:
function ultimateReverse (array) {
let newArray = array.join("").split("");
let reversedArray = newArray.reverse();
return reversedArray.join("");
}
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
//!eiltonnacIdnasttubgibekilI
But the outcome that we want is:
["!", "eilt", "onn", "acIdn", "ast", "t", "ubgibe", "kilI"]
So according to the original array, the length of the first element should be 1, the length of the second element should be 4, the third should be length 3, and so on...
Is there a way to split a string into array of elements, each of a specified length?
I thought of creating an array of the lengths of the items from the original array by doing:
function ultimateReverse (array) {
let elementLengths = [];
let newArray = array.join("").split("");
let reversedArray = newArray.reverse().join("");
for (let i = 0; i < array.length; i++) {
let element = array[i];
elementLengths.push(element.length);
}
return reversedArray + " " + elementLengths;
}
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
//!eiltonnacIdnasttubgibekilI 1,4,3,5,3,1,6,4
Now if I can just split the string into elements in an array based on the length of the original elements...
{length}
on line 2 - length is an object, in this case? – Philipson