我正在学习JS,我遇到了一个目前无法解决的挑战。我必须像这样的数组中使用字母... var alphabet = ['abcdefghijklmnopqrstuvwxyz'];
或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'];
。我需要console.log()
字母表作为休闲......
a
ab
abc
abc d
abc de
abc def
abc def g
abc def gh
abc def ghi
到目前为止,我有以下代码......
function stackLetter(l) {
for (var index = 1; index < l[0].length; index++) {
console.log(l[0].slice(0, index));
}
}
stackLetter(alphabet);
到目前为止的结果......
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
我还在MDN上阅读有关字符串操作的内容,但到目前为止还无法使其工作。我如何调整我的代码来完成结果?我想用香草JS做这个。
答案 0 :(得分:3)
您可以在每隔三个字母后为字符串添加一个空格:
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
}
}
答案 1 :(得分:1)
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);
}
}
答案 2 :(得分:0)
类似的东西:
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
结果:
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
...或使用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('') ) )
}
答案 3 :(得分:0)
我知道这有一个公认的解决方案,但我认为这个解决方案更简单 - 而不是遍历循环并每次应用间距逻辑 - 做一个并在每个第三个元素之前插入空格创建一个带空格的字符串。然后遍历列表并显示一个子字符串,其中包含越来越多的修改后的字符串,并带有换行符以给出堆叠样式。
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
}
&#13;