以下面的格式在javascript中显示金字塔

时间:2016-05-25 17:00:45

标签: javascript

我试图仅使用javascript在控制台部分显示金字塔,除了document.write(“”)

 *                                                       
 * *                                                              
 * * *                                                      
 * * * *                                                     
 * * * * *

我不希望采用上述格式。 我想用以下格式

    *                                                       
   * *                                                              
  * * *                                                      
 * * * *                                                     
* * * * *

我的代码是

function getPyramid(param) {

  
      for (var i = 1; i <= param; i++) {
var output = "";
        for (var j = 1; j <= i; j++) {
          output += j + "     ";
        }
        console.log(output);
        output = "";
      }

    }
<button onclick="getPyramid('10')">

4 个答案:

答案 0 :(得分:0)

    function renderPyramid(n) {
      for (var i = 0; i < n; i++) {
        var str = '';
        for (var j = 1; j < n-i; j++) {
          str = str + ' ';
        }
        for (var k = 1; k <= (2*i+1); k++) {
          str = str + '*';
        }
        console.log(str);
      }
    }
    renderPyramid(5)

答案 1 :(得分:0)

我的解决方案

function pyramid(n) {
    // generate base of pyramid, aka longest possible string
    let limit = n+n-1;

    let hashesToPrint = 1; // number of hashes to print
    for (let i=0; i<n; i++) {
        
        // get length of spaces we need on each side
        let difference = (limit - hashesToPrint) / 2;
        
        // generate spaces string
        let spaces = ' '.repeat(difference);

        // create pounds string
        let pounds = '#'.repeat(hashesToPrint);

        // append spaces on either side of our pound string
        let newString = spaces + pounds + spaces

        console.log(newString)

        // increment our counter by two
        hashesToPrint += 2
    }
}

pyramid(3)


答案 2 :(得分:-1)

已解决:我得到了这个问题的逻辑

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<button onclick="getPyramid('10')">
Get Pyramid</button>


<script>

function getPyramid(param)
{



for(var i=0;i<param;i++) {
var output="";
    for(var j=0;j<param-i;j++) {
        output+=" ";
        // console.log(" ");
    }
    for(var k=0;k<=i;k++) {
       // console.log("* ");
       output += "* ";
    }
    // output += "";
    console.log(output);  
}


}

</script>
</body>
</html>

答案 3 :(得分:-1)

<script type="text/javascript">
var i, j;

for (i = 1; i <= 5; i++)

{

  for (j = 1; j <= i; j++)


  {

    document.write("\t*");

  }

  document.write("<br>");

}

for (i = 5; i >= 1; i--)


{

  for (j = 1; j <= i; j++)

  {
    document.write("\t*");

  }

  //document.write("\t");
  document.write("<br>");
}


for (i = 5; i >= 0; i--)

{
  for (j = 0; j < i; j++)

  {
    document.write("*");

  }

  for (k = i; k <= 5; k++)

  {

    document.write("*");
  }


  document.write("*");

}
</script>