admin管理员组文章数量:1316393
I'm trying to build a triangle using nested loops.
var line, triangle;
line = triangle = "";
for (var row = 1; row <= 7; row++) {
for (var x = 0; x < row; x++) {
line += "#";
}
triangle += line + "\n";
}
console.log(triangle);
I'm trying to build a triangle using nested loops.
var line, triangle;
line = triangle = "";
for (var row = 1; row <= 7; row++) {
for (var x = 0; x < row; x++) {
line += "#";
}
triangle += line + "\n";
}
console.log(triangle);
I expected that each row is more than the last by only one "#" like this:
But this is the result I got:
- 1 You probably want to start with an empty line. – SLaks Commented Dec 7, 2017 at 20:12
- 1 You shouldn't need two loops for this. Keep two variables, one for the result and another for the current row. Append the current row to the result each loop – will.fiset Commented Dec 7, 2017 at 20:15
- @will.fiset I'll try that – Moaaz Bhnas Commented Dec 7, 2017 at 20:26
6 Answers
Reset to default 2The reason your code does it is you are updating line
on each iteration and you keep appending to it. If you want to do the nested loops, than you need to reset the variable line each time you are in the outer loop.
var line, triangle;
line = triangle = "";
for (var row = 1; row <= 7; row++) {
line =""
for (var x = 0; x < row; x++) {
line += "#";
}
triangle += line + "\n";
}
console.log(triangle);
Or you can keep what you have and dump the inner loop and every iteration you just add one character to the line.
var line, triangle;
line = triangle = "";
for (var row = 1; row <= 7; row++) {
line += "#";
triangle += line + "\n";
}
console.log(triangle);
You need to empty your line before each nested iteration. Without this you have one line and every time concatenate new items to it. Also you can leave the line
variable and just use the triangle
.
var triangle = '';
for (var row = 1; row <= 7; row++) {
for (var x = 0; x < row; x++) {
triangle += "#";
}
triangle += "\n";
}
console.log(triangle);
You can also try this solution with String#repeat
var triangle = '';
for (var row = 1; row <= 7; row++) {
triangle += '#'.repeat(row) + '\n';
}
console.log(triangle);
function triangle(num) {
for(let i = '#'; i.length < num; i+='#') {
console.log(i)
}
}
Try code below:
function generatePyramid() {
var totalNumberofRows = 7;
var output="";
for (var i = 1; i <= totalNumberofRows; i++) {
for (var j = 1; j <= i; j++) {
output+= "# ";
}
print(output);
output="";
}
}
generatePyramid();
How it works: http://rextester./ULY85622
EDIT: Fixed it by just adding one line - you need to reinitialize the variable "line" after each row iteration
var line, triangle;
line = triangle = "";
for (var row = 1; row <= 7; row++) {
line="";
for (var x = 0; x < row; x++) {
line += "#";
}
triangle += line + "\n";
}
console.log(triangle);
You need to reset line
after each loop because it is accumulating all #
on each cycle:
var line, triangle;
line = triangle = "";
for (var row = 1; row <= 7; row++) {
line = "" // Add this line
for (var x = 0; x < row; x++) {
line += "#";
}
triangle += line + "\n";
}
console.log(triangle);
Your code is incorrect because line follows the following steps on each cycle:
- row 1:
line
starts as '' and ends as#
(Adds#
) - row 2:
line
starts as#
and ends as###
(Adds##
) - row 3:
line
starts as###
and ends as######
(Adds###
) - row 4:
line
starts as######
and ends as##########
(Adds####
) - row 5:
line
starts as##########
and ends as###############
(Adds#####
) - row 6:
line
starts as###############
and ends as#####################
(Adds######
) - row 7:
line
starts as#####################
and ends as############################
(Adds#######
)
本文标签: javascriptBuilding triangle with nested loopsStack Overflow
版权声明:本文标题:javascript - Building triangle with nested loops - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742003017a2411397.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论