[英]Can somebody explain to me why we need a for loop in this function?
I am relatively new to programming, and was reading the book, Eloquent Javascript today. 我对编程还比较陌生,并且正在阅读《 Eloquent Javascript》这本书。 I came across an example that was related to overall scope.
我遇到了一个与总体范围有关的示例。 However I was more interested in understanding all of the elements within the function that they provided.
但是,我对理解它们提供的功能中的所有元素更感兴趣。
Here is the function: 这是函数:
var landscape = function () {
var result = "";
var flat = function (size) {
for (var count = 0; count < size; count++)
result += "_";
};
var mountain = function (size) {
result += "/";
for (var count = 0; count < size; count++)
result += "'"
result += "\\"
};
flat(3);
mountain(4);
flat(6);
mountain(1);
return result;
};
console.log(landscape());
As I was trying to break down this function, I understood mostly everything except for the for loops. 在尝试分解此函数时,我几乎了解了所有内容,除了for循环。 I am not sure why they are needed for this function, and was was wondering if somebody would be willing to digest this problem for me, and explain why we need for loops to make the function work properly.
我不确定为什么需要此功能,并且想知道是否有人愿意为我解决这个问题,并解释为什么我们需要使用循环来使功能正常工作。
Right now you're getting the output: 现在,您将获得输出:
___/''''\______/'\
Without them you'd get two functions looking like this (simplified): 没有它们,您将获得两个看起来像这样的功能(简化):
var flat = function(size){
result += "_";
};
var mountain = function(size){
result += "/'\\";
};
Hence you'd get the output 因此,您将获得输出
_/'\_/'\
The size
argument for flat()
defines how many underscores should be output, and the size
argument for mountain
defines how many single-quotes ( '
) should be output. flat()
的size
参数定义应输出多少个下划线,而mountain
的size
参数定义应输出多少个单引号( '
)。
Hence you need the loops, or else you'll get smaller mountains and flats. 因此,您需要循环,否则您将得到较小的山脉和平坦的土地。
Consider the below "drawing" with F
being "flat" and M
being "mountain": 考虑下面的“图形”,其中
F
为“平坦”, M
为“山”:
___/''''\______/'\
FFFMMMMMMFFFFFFMMM
Every mountain is always a minimum of 3 characters long (as it outputs a forward slash, at least one '
and then a backslash). 每个山峰总是至少3个字符长(因为它输出正斜杠,至少输出一个
'
,然后输出反斜杠)。 This means that the above output is (along with your code) 这意味着上面的输出是(连同您的代码)
3 x F = 3 flats | flats(3)
6 x M = (6 - 2) = 4 mountains | mountain(4)
6 x F = 6 flats | flats(6)
3 x M = (3 - 2) = 1 mountain | mountain(1)
To reiterate: 重申:
// define an anonymous function with a "size" parameter and save it as "flat"
var flat = function (size) {
// start "count" at 0 (count = 0) and add 1 to "count" (count++) while "count" is less than "size" (count < size)
for (var count = 0; count < size; count++) {
// Add an underscore to the "result" variable
result += "_";
}
};
// define an anonymous function with a "size" parameter and save it as "mountain"
var mountain = function (size) {
// Add a forward slash to the "result" variable
result += "/";
// start "count" at 0 (count = 0) and add 1 to "count" (count++) while "count" is less than "size" (count < size)
for (var count = 0; count < size; count++) {
// Add a single quote to the "result" variable
result += "'";
}
// Add a backwards slash to the "result" variable
result += "\\"
};
If you're asking because 如果你问是因为
if (foo)
bar();
oof();
Seems weird to you, then always consider a single line condition like this to be similar to 对您来说似乎很奇怪,然后总是考虑这样的单行条件类似于
if (foo) {
bar();
}
oof();
Like said in the other answer the for loop is used to concatenate the characters a specified number of times. 就像在其他答案中所说的,for循环用于将字符连接指定的次数。 I want to point out that you don't need the for loops to make your "flats" and "mountains".
我想指出的是,您不需要for循环即可创建“单位”和“山”。
var landscape = function () {
var result = "";
var flat = function (size) {
result += Array(size+1).join("_");
};
var mountain = function (size) {
result += "/";
result += Array(size+1).join("'");
result += "\\";
};
flat(3);
mountain(4);
flat(6);
mountain(1);
return result;
};
console.log(landscape());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.