-2

I have an array of strings:

['braid', 'write', 'pence', 'schmo']

I would like some method to transform this into this:

['bwps', 'rrec', 'ainh', 'itcm', 'deeo']

I.e. I want the new strings to be the combination of all 1st letters, 2nd letters, 3rd letters, etc. of the old strings.

Plain Vanilla JS is preferred.

ChrisC
  • 892
  • 2
  • 12
  • 33
  • What have you tried, where are you stuck? – deceze Jul 16 '23 at 11:06
  • 1
    @deceze Why does this question need more focus? - it asks for one thing at a time, take the existing words, and combine them into a new one...? – Sally loves Lightning Jul 16 '23 at 11:06
  • 1
    Please read the [Open letter to students with homework problems](https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems) and [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822). – Quentin Jul 16 '23 at 11:07
  • @Quentin this is not homework, thanks – ChrisC Jul 16 '23 at 11:09
  • @LightningMcQueen Because "I have X, I want Y, fill in the blank" isn't a technical question. It's a task description. It can be completed by writing code that does the thing. Like any other programming task. There's no question or technical problem that OP has mentioned that needs solving. – deceze Jul 16 '23 at 11:12
  • @deceze: and this question is fine? https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array I see no difference – ChrisC Jul 16 '23 at 11:20
  • https://meta.stackoverflow.com/q/367913/476 – deceze Jul 16 '23 at 11:23

1 Answers1

1

So, first, calculate the maximum length of the array.

Then, create a new array of the maximum length and maps each position, extracting characters at index from each string, and joining them into a transformed string.

const words = ['braid', 'write', 'pence', 'schmo'];
const combine = Array.from({ length: Math.max(...words.map(({ length }) => length)) }, (_, i) => words.map(s => s[i] || '').join(''));
console.log(combine);