【问题标题】:display pyramid in javascript in the below format以以下格式在 javascript 中显示金字塔
【发布时间】:2016-05-25 17:00:44
【问题描述】:

我试图在控制台部分显示金字塔,只使用除 document.write("") 之外的 javascript

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

我不想要上述格式。 我想要以下格式

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

我的代码是

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 = "";
      }

    }
&lt;button onclick="getPyramid('10')"&gt;

【问题讨论】:

  • 很好。你的问题到底是什么?
  • 我想使用 javascript 准确显示中心金字塔。
  • 这仍然不是问题。这是你的待办事项清单。
  • @Marc 我已经编辑了我的问题。请检查一下
  • 这不是问题,是要求我们为你做作业。

标签: javascript


【解决方案1】:

    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)

【讨论】:

  • 最好加一些解释,或者在代码中加入适当的cmets。
【解决方案2】:

我的解决方案

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)


【讨论】:

    【解决方案3】:

    已解决:我有这个问题的逻辑

    <!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>
    

    【讨论】:

      【解决方案4】:
      <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>
      

      【讨论】:

        猜你喜欢
        • 2014-01-11
        • 2018-06-06
        • 2021-08-20
        • 2013-12-06
        • 1970-01-01
        • 1970-01-01
        • 2014-06-05
        • 2020-07-13
        相关资源
        最近更新 更多