首页 > 解决方案 > 如何根据某些规则制作一个中间有空格的字符串

问题描述

我正在编码一组字符串,在输出时,我想得到一个由空格分隔的字符串,像这样

“imtgdvs 恐惧者 mayoogo anouuio ntnnlvt wttddes aohghn sseoau”

代替

“imtgdvs”

“恐惧者”

“梅优果”

“阿努伊奥”

“ntnnlvt”

“wtddes”

“啊啊啊”

“sseo”

normalisedText = "ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots";
const cols = Math.ceil(Math.sqrt(textCount));
const rows = Math.ceil(textCount / cols);
const textArray = [];
let encodedChunks = "";
let cypherText = "";
let startIndex = 0;

for (let i = 0; i < cols; i ++) { 
  for (let j = i; j < normalisedText.length; j += cols) {
  cypherText += normalisedText[j];
  }
    cypherText += '\n';
}

console.log(cypherText);

标签: javascript

解决方案


尝试这个:

textCount = 5
normalisedText = "ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots";
const cols = Math.ceil(Math.sqrt(textCount));
const rows = Math.ceil(textCount / cols);
const textArray = [];
let encodedChunks = "";
let cypherText = "";
let startIndex = 0;

for (let i = 0; i < cols; i ++) { 
  for (let j = i; j < normalisedText.length; j += cols) {
  cypherText += normalisedText[j];
  }
    cypherText += ' ';
}

console.log(cypherText);


推荐阅读