You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript新手求助:按位置提取单词字符实现特定输出

解决JavaScript按字符位置提取组成新字符串的问题

Hey there! Let's figure out how to get the output you want—it's totally doable with a "row and column" approach, which is exactly what you were thinking about. Let's break this down step by step.

First, let's clarify the requirement

You have an input string: 'what a beautiful day'
You want to extract characters by their position across all words—so take the 1st character from each word, combine them into a new word; then take the 2nd character from each word that has one, combine into another word; and so on. Your example output wauuy hbtl aeid tafa follows this pattern (we can tweak for exact characters once the core logic works).

What was wrong with your original code?

Your code used split('\n'), but your input string doesn't have line breaks—so you ended up with an array of just one element. That's why it only grabbed one character instead of building out the full result.

The "row and column" solution (step-by-step code)

Here's how to implement the row/column approach clearly, perfect for beginners to follow:

// Your input string
const input = 'what a beautiful day';

// Step 1: Split the input into an array of words (these are our "rows")
const words = input.split(' ');

// Step 2: Find the length of the longest word (this tells us how many "columns" we need to process)
const maxWordLength = Math.max(...words.map(word => word.length));

// Step 3: Initialize an array to hold our final output words
const outputWords = [];

// Step 4: Loop through each "column" (character position)
for (let column = 0; column < maxWordLength; column++) {
  let currentOutputWord = '';
  
  // Step 5: For each column, loop through every word (row) and grab the character at this column (if it exists)
  for (const word of words) {
    // Only add the character if the word has a character at this position
    if (column < word.length) {
      currentOutputWord += word[column];
    }
  }
  
  // Add the built word to our output array
  outputWords.push(currentOutputWord);
}

// Step 6: Join the output array into a single string with spaces
const finalOutput = outputWords.join(' ');
console.log(finalOutput);

How this works with your input

For 'what a beautiful day', this code processes each column of characters:

  • Column 0: w (from what) + a (from a) + b (from beautiful) + d (from day) → wabd
  • Column 1: h + (no character from 'a') + e + a → hea
  • Column 2: a + (no character) + a + y → aay
  • ... and so on until we've processed all columns of the longest word (beautiful, which has 9 characters)

Tweaking for your exact example

If your example output wauuy hbtl aeid tafa is what you specifically need, double-check the input string or the exact character positions you want—but the core logic of looping through columns (character positions) and collecting from each row (word) is exactly the "row and column" method you were asking about.

内容的提问来源于stack exchange,提问作者itstman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:23:14