最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Reverse Array, Let Elements in New Array Equal Length of Original Array Elements - JavaScript - Stack Overflow

programmeradmin4浏览0评论

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 oute 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...

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 oute 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...

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked May 19, 2019 at 3:28 HappyHands31HappyHands31 4,10119 gold badges65 silver badges117 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

First construct the full reversed string, eg

!eiltonnacIdnasttubgibekilI

Then, from an array of the initial lengths (which can be done with a .map in advance), iterate over that array and slice that length from the reversed string, and push to an array:

function ultimateReverse(array) {
  const lengths = array.map(({ length }) => length);
  let reversedStr = [...array.join("")].reverse().join('');
  const result = [];
  lengths.forEach((length) => {
    result.push(reversedStr.slice(0, length));
    reversedStr = reversedStr.slice(length);
  });
  return result;
}

console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));

You could also keep the initial reversed data as an array that you splice from, instead of reassigning reversedStr:

function ultimateReverse(array) {
  const lengths = array.map(({ length }) => length);
  const reversedChars = [...array.join('')].reverse();
  return lengths.map(
    length => reversedChars.splice(0, length).join('')
  );
}

console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));

You can also solve this via revering the string then mapping through it and essentially "cutting it to pieces" with substr like this:

let reverse = arr => {	
  let i = 0, rData = [...arr.join('')].reverse().join('')
  return arr.map(x => {
    let str = rData.substr(i, x.length)
    i += x.length 
    return str
  })
}

console.log(reverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]))

Basically map through the original array and for each string substr from the bined reversed string as much as the current x length is.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论