Let's first try to write a function for just one line of the pyramid, any line.(首先,让我们尝试为金字塔的仅一行,任意行编写一个函数。)
We'll ignore the whitespace for now too.(我们现在也将忽略空格。)
Every line of the pyramid seems to follow a few simple rules:(金字塔的每一行似乎都遵循一些简单的规则:)
- All lines start with a
*
.(所有行均以*
开头。)
- The 'inner sequence' is just alternating
1
and 0
a certain number of times.(“内部序列”只是交替1
和0
一定次数。)
- There are
2*n - 1
digits in the inner sequence of each line, except the first.(除第一行外,每行的内部序列中都有2*n - 1
位数字。)
- Even lines (0, 2, 4, ...) begin the inner sequence with a
1
.(偶数行(0,2,4,...)以1
开头内部序列。)
- Odd lines (1, 3, 5, ...) begin the inner sequence with a
0
.(奇数行(1、3、5,...)以0
开始于内部序列。)
- All lines end with an additional
*
, except the first.(除第一行外,所有行均以附加*
结束。)
In the rules above, everything can be determined from n
.(在上述规则中,一切都可以从n
确定。)
So our function only needs one argument:(所以我们的函数只需要一个参数:)
function GetLine(n) {
// ...
}
We can also determine whether a line is even or odd, and how many characters the inner sequence has:(我们还可以确定一行是偶数还是奇数,以及内部序列有多少个字符:)
EvenLine = (n % 2 == 0);
Count = 2*n - 1;
Building the sequence between the two stars can be done with a simple for
loop.(可以使用简单的for
循环在两颗星之间建立序列。)
Putting that all together, we can build the following function.(综上所述,我们可以构建以下功能。)
function GetLine(n) {
// Create a string for the line that starts with a star
var Line = "*";
// Determine whether the line is even or odd
var EvenLine = (n % 2 == 0);
// Calculate the total number of ones and zeros in the line
var Count = (2 * n - 1);
// We need a variable to store whether the next character should be a one or zero
var One = EvenLine ? true : false; // Even lines start with 1, Odd starts with 0
// Repeat 'Count' times, alternating between ones and zeros
for (var i=0; i<Count; i++)
{
Line += One ? "1" : "0";
One = !One; // Toggle the bool value to alternate on the next iteration
}
// Only add a tail star if we're not on the first line
if (n > 0) Line += "*";
return Line;
}
When we call GetLine()
with some consecutive numbers we can see that the pattern is mostly there:(当我们使用一些连续的数字调用GetLine()
,我们可以看到该模式主要存在:)
console.log(GetLine(0));
console.log(GetLine(1));
console.log(GetLine(2));
*
*0*
*101*
Now all that needs to be done is insert the whitespace in two steps:(现在,需要做的就是分两步插入空白:)
- A space between each character.(每个字符之间的空格。)
- The leading space to align the pyramid.(对齐金字塔的引导空间。)
function printPyramid(s) {
for (var n=0; n<s; n++) {
// Get the line for n
var line = GetLine(n);
// This one-liner can be used to insert whitespace between each character
// split('') will explode the characters into an array
// join(' ') will turn the array back into a string with ' ' inbetween each element
line = line.split('').join(' ');
// Then we just add the necessary whitespace for alignment
// We need 2 * (s - n - 1) spaces in front of each line.
line = " ".repeat(s - n - 1) + line;
// Print the line
console.log(line);
}
}
Finally,(最后,)
printPyramid(5);
*
* 0 *
* 1 0 1 *
* 0 1 0 1 0 *
* 1 0 1 0 1 0 1 *
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…