简体   繁体   English

Console.log 字母,每 3 个字母带有空格 javascript

[英]Console.log alphabet with space every 3 letters javascript

I'm learning JS and I have a challenge that I can't solve so far.我正在学习 JS,但我遇到了迄今为​​止无法解决的挑战。 I have to have the alphabet in an array like so... var alphabet = ['abcdefghijklmnopqrstuvwxyz'];我必须像这样在数组中使用字母表... var alphabet = ['abcdefghijklmnopqrstuvwxyz']; or var alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];var alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']; . . I need to console.log() the alphabet as fallow...我需要console.log()字母表作为休耕......

a
ab
abc
abc d
abc de
abc def
abc def g
abc def gh
abc def ghi

So far I have the following code...到目前为止,我有以下代码...

function stackLetter(l) {
    for (var index = 1; index < l[0].length; index++) {
        console.log(l[0].slice(0, index));
    }
}


stackLetter(alphabet);

The outcome so far...到目前为止的结果...

a
ab
abc
abcd
abcde
abcdef
abcdefg
abcdefgh
abcdefghi
abcdefghij
abcdefghijk
abcdefghijkl
abcdefghijklm
abcdefghijklmn
abcdefghijklmno
abcdefghijklmnop
abcdefghijklmnopq
abcdefghijklmnopqr
abcdefghijklmnopqrs
abcdefghijklmnopqrst
abcdefghijklmnopqrstu
abcdefghijklmnopqrstuv
abcdefghijklmnopqrstuvw
abcdefghijklmnopqrstuvwx
abcdefghijklmnopqrstuvwxy

I'm still reading in the MDN about string manipulation but can't make it to work so far.我仍在 MDN 中阅读有关字符串操作的内容,但到目前为止还无法使其正常工作。 How I can tweak my code to accomplish the outcome?我如何调整我的代码来完成结果? I want to do this with vanilla JS.我想用香草 JS 做到这一点。

You can add a space to your string after every 3rd letter:您可以在每第三个字母后为字符串添加一个空格:

var alphabet = ['a', 'b'...'y', 'z'];

function stackLetters(alphaArr) {
  var stackResult = ''; //initialize as empty string
  for (var i = 0; i < alphaArr.length; i++) { //iterate thru alphabet array
    if (i % 3 === 0 && i !== 0) { //every 3rd element
      stackResult += ' ' + alphaArr[i]; //prepend space and concatenate result
    } else {
      stackResult += alphaArr[i]; //concatenate result
    }
    console.log(stackResult); //log during each iteration current stackResult
  }
} 
function stackLetter(l) {
    for (var index = 1; index < l[0].length; index++) {
        var str = "";
        for(var j = 0; j < index; j++)
        {
            str += l[0][j];
            if(j % 3 == 0 && j != 0)
                str += " ";
        }
        console.log(str);
    }
}

Something like that:类似的东西:

var alphabet = 'abcdefghijklmnopqrstuvwxyz';

function stackLetter(l) {
    l.forEach( function(l1, i1) {
        var str = '';
        for (var i2=0; i2<=i1; i2++) {
            str += l[i2]
            if ((i2+1) % 3 == 0) str += ' ';
        };
        console.log(str);
    } );
}


stackLetter(alphabet.split('')); //or pass the array directly

Results:结果:

a
ab
abc 
abc d
abc de
abc def 
abc def g
abc def gh
abc def ghi 
abc def ghi j
abc def ghi jk
abc def ghi jkl 
abc def ghi jkl m
abc def ghi jkl mn
abc def ghi jkl mno 
abc def ghi jkl mno p
abc def ghi jkl mno pq
abc def ghi jkl mno pqr 
abc def ghi jkl mno pqr s
abc def ghi jkl mno pqr st
abc def ghi jkl mno pqr stu 
abc def ghi jkl mno pqr stu v
abc def ghi jkl mno pqr stu vw
abc def ghi jkl mno pqr stu vwx 
abc def ghi jkl mno pqr stu vwx y
abc def ghi jkl mno pqr stu vwx yz

...or another approach using ES6 and regex (just for fun): ...或另一种使用 ES6 和正则表达式的方法(只是为了好玩):

function stackLetter(_alphabet) { //receives a string

    _alphabet
        .match(/.{1,3}/g)
        .map((c) => c + ' ')
        .join('').split('')
        .forEach( (a,b,c) => console.log(c.slice(0,b).join('') ) )

}

I know this has an accepted solution, but I think this solution is more simple - rather than iterating through the loop and applying the spacing logic each time - do it one and insert the space before each third element creating a string with spaces.我知道这有一个公认的解决方案,但我认为这个解决方案更简单 - 而不是遍历循环并每次应用间距逻辑 - 做一个并在创建一个带空格的字符串的每个第三个元素之前插入空格。 Then iterate through the list and display a substring with an increasing number of the modified string with a line break to give the stacked style.然后遍历列表并显示一个子字符串,其中修改后的字符串数量不断增加,并带有换行符以给出堆叠样式。

 var alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']; var str = '', spacer; for(i=0;i<alphabet.length;i++){ if(i % 3 == 0){var spacer = ' '} else {var spacer=''}; str += spacer + alphabet[i]; }; console.log(str); //gives abc def ghi jkl mno pqr stu vwx yz for(a=0;a<=str.length;a++){ var res = str.substr(0, a); console.log(res); //give results such as abc def ghi jkl mno }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM