简体   繁体   English

我怎样才能使buildTriangle(lines)建立一个矩形

[英]How can I make buildTriangle(lines) build a rectangle

I am currently taking the Udacuty intro to Javascript course. 我目前正在参加Udacuty Java语言入门课程。 This quiz that I have completed from the course, makes you create a function that takes a number and build a triangle using the number it has taken as the triangle widest width. 我已完成本课程的测验,使您创建了一个使用数字的函数,并使用该数字作为三角形最宽的宽度来构建一个三角形。

My question, is how can I change the code of the function buildTriangle(lines) to make function makeLine(lenghth) build a Square instead? 我的问题是,如何更改功能buildTriangle(lines)的代码,以使功能makeLine(lenghth)改为构建Square?

Thanks for your help. 谢谢你的帮助。

 function makeLine(length) { var line = ""; for (var j = 1; j <= length; j++) { line += "* "; } return line + "\\n"; } function buildTriangle(lines){ var triangle = ""; for(i = 1; i <= lines; i++){ triangle += makeLine(i); } return triangle; } console.log(buildTriangle(10)); 

triangle += makeLine(lines); instead of triangle += makeLine(i); 而不是triangle += makeLine(i);

 function makeLine(length) { var line = ""; for (var j = 1; j <= length; j++) { line += "* "; } return line + "\\n"; } function buildTriangle(lines){ var triangle = ""; for(i = 1; i <= lines; i++){ triangle += makeLine(lines); } return triangle; } console.log(buildTriangle(10)); 

Right now each time it creates a new line it looks at the "i" from the for loop to tell it how long it should make it. 现在,每次创建新行时,它都会在for循环中查看“ i”,以告诉它应该持续多长时间。 This means everytime it will be one longer and create a triangle. 这意味着每次将更长并创建一个三角形。 If you want it to create a square, you want every line to the same length as the height of the square. 如果要创建一个正方形,则希望每条线的长度与正方形的高度相同。 so you want to change it from triangle += makeLine(i); 所以你想从triangle += makeLine(i); to triangle += makeLine(lines); triangle += makeLine(lines); . That way it every time they make a new line it's the same length as the height (lines). 这样,他们每次换行时,其长度便与高度(线)相同。

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

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