我如何根据某些规则制作一个字符串,并在字符串之间使用空格

时间:2019-06-24 10:14:26

标签: javascript

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

  

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

代替

“ imtgdvs”

“恐惧者”

“ mayoogo”

“ anouuio”

“ ntnnlvt”

“ wttddes”

“ aohghn”

“ sseoau”

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);

1 个答案:

答案 0 :(得分:0)

尝试一下:

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);