简体   繁体   English

使用javascript函数打印三角形

[英]Print Triangle using javascript function

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

I am trying to print triangle, i dont know where i am doing wrong, can some please explain the logic behind printing triangle using nested loops我正在尝试打印三角形,我不知道我在哪里做错了,有人可以解释一下使用嵌套循环打印三角形背后的逻辑吗

*
**
***

After you finish printing a line, you need to add a newline "\n" so that you move to the next line.打印完一行后,您需要添加一个换行符“\n”以便移动到下一行。 You could do this as below :你可以这样做:

 function makeLine(length) { // length has the number of lines the triangle should have var line = ""; for (var i = 1; i <= length; i++) { // Enter the first for loop for the number of lines for(var j=1; j<=i; j++){ // Enter the second loop to figure how many *'s to print based on the current line number in i. So the 1st line will have 1 *, the second line will have 2 *s and so on. line += "*"; } // Add a newline after finishing printing the line and move to the next line in the outer for loop line+="\n"; } // Print an additional newline "\n" if desired. return line + "\n"; } console.log(makeLine(2));

don't forget about .repeat()不要忘记.repeat()

 function makeLine(length) { var line = ""; for (var i = 1; i <= length; i++) { line+="*".repeat(i)+"\n"; } return line; } console.log(makeLine(3));

The \n was at an incorrect position. \n的位置不正确。

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

 function makeLine(length) { var line = ""; for (var i = 1; i <= length; i++) { for (var j = 1; j <= i; j++) { line += "*"; } // add new line after loop is completed line = line + "\n" } return line + "\n"; } console.log(makeLine(5));

you need to add \n to line when the inner loop is completed内循环完成后,您需要将\n添加到行

function hashTriangle(length)
{
let str="";
for(let i=0;i<length;i++) 
    {

    str+="#";
    console.log(str);
    }
}
hashTriangle(7);

console.log() prints a new line. console.log() 打印一个新行。 So it is not necessary for nested loops and confusing newline characters to be appended to our string.因此,没有必要将嵌套循环和令人困惑的换行符附加到我们的字符串中。

Simple solution using padStart , padEnd , repeat method for printing right and left triangle使用padStartpadEnd重复打印左右三角形的简单解决方案

Left triangle左三角形

 const printLeftTriangle = (n) => { let output=''; for (let i = 1; i <= n; i++) { output +="*".repeat(i).padStart(n) + "\n"; } return output; } console.log(printLeftTriangle(5));

Right triangle直角三角形

 const printRightTriangle = (n) => { let output=''; for (let i = 1; i <= n; i++) { output +="*".repeat(i).padEnd(n) + "\n"; } return output; } console.log(printRightTriangle(5));

try this solution please:请尝试此解决方案:

const halfTriangle = N => {
  for (let row = 0; row < N; row++) {
    let line = "";
    for (let col = 0; col <= N; col++) {
      if (col <= row) {
        line += '#';
      } else {
        line += ' '
      }
    }
    console.log(line);
  }
}

halfTriangle(4)

 // creates a line of * for a given length function makeLine(length) { let line = ""; for (var j = 1; j <= length; j++) { line += "* "; } return line + "\n"; } // your code goes here. Make sure you call makeLine() in your own code. function buildTriangle(length) { // Let's build a huge string equivalent to the triangle var triangle = ""; //Let's start from the topmost line let lineNumber = 1; for (lineNumber = 1; lineNumber <= length; lineNumber++) { // We will not print one line at a time. // Rather, we will make a huge string that will comprise the whole triangle triangle = triangle + makeLine(lineNumber); } return triangle; } // test your code console.log(buildTriangle(10));

Center Tringle中心三角

 let line=''; for(let i=1; i<=5;i++){ line += ' '.repeat(5-i) line += '*'.repeat(i+i-1)+'\n' } console.log(line);

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

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