how to Display Pyramid using Javascript?

2020-01-27 07:14发布

Here is the code to display PYRAMID but its not exactly producing required output.

function generatePyramid() {
    var totalNumberofRows = 5;
    var arr = new Array();
    for (var i = 1; i <= totalNumberofRows; i++) {
        for (var j = 1; j <= i; j++) {

            arr.push(j);
            console.log(j);
        }
        console.log("\n");
    }
}

Required Output

18条回答
趁早两清
2楼-- · 2020-01-27 07:41

Simple code of Number Pyramid

for(var i=1; i<=5; i++){
 var Num='';
 for(var j=0; j<i; j++){
     Num += i;
 }
 print(Num) }
查看更多
戒情不戒烟
3楼-- · 2020-01-27 07:42

I would stick to recursive approach in such a case:

function generatePyramid (n, row = 0, line = '', number = 1) {
  
    if(row === n){
      return;
    }
    
    if (line.length === n) {
        console.log(line )
        return generatePyramid (n, row + 1)
    } 
    
    if (line.length <= row) {
        line += number;
    } else {
        line += ' ';
    }
    
    generatePyramid (n, row, line, number + 1)
  }

查看更多
兄弟一词,经得起流年.
4楼-- · 2020-01-27 07:42

Assuming you want to return numbers and not asterisks as the other answers show, here is that solution:

Note that this solution runs in linear (O(n)) time complexity and doesn't sacrifice performance by logging every line to the console, but the entire pyramid at once instead.

function generatePyramid(n) {
  let pyramid = '';
  let prev;

  for (let i = 1; i <= n; i++) {
    if (prev) {
      pyramid += '\n';
      prev =  prev + ' ' + i;
    } else {
      prev = i;
    }
    pyramid += ' '.repeat(n - i) + prev;
  }

  return pyramid;
}

Log to the console as such: console.log(generatePyramid(n));

If you're looking to just draw a triangle as the picture in your question shows, this function will do that (again, in linear time complexity):

function drawTriangle(n) {
  let triangle = '';
  let prev;
  for (let i = 1; i <= n; i++) {
    if (prev) {
      triangle += '\n';
      prev = prev + ' ' + i;
    } else {
      prev = i;
    }
    triangle += prev;
  }
  return triangle;
}
查看更多
爷的心禁止访问
5楼-- · 2020-01-27 07:45
  const pyramid = (n)=>{
  const mid = Math.floor((2*n-1)/2);
  for(let row=0; row<n; ++row)
  {
    //for each row, make empty steps
    let level = ''
    for(let col=0; col<2*n-1; col++)
    {
      if(mid-row <=col && mid+row >= col)
        level+='#';
      else level +=' ';
    }
    console.log(level);
  }
}
pyramid(3);
查看更多
\"骚年 ilove
6楼-- · 2020-01-27 07:45
function pyramid() {
    var n = 5;
    var output="";
    for (var i = 0; i <n; i++) {
    var myspace = "";   
    for(let s = 0; s <(n-i-1); s++) {
        myspace += " ";
    }
        for (var j = 1; j <= 2*i + 1; j++) {
            output+="*";

        }
        console.log(myspace+output);
        output="";
    }
}

Output

            *
VM74:11    ***
VM74:11   *****
VM74:11  *******
VM74:11 ********* 
查看更多
可以哭但决不认输i
7楼-- · 2020-01-27 07:46

Try the below code

function generatePyramid() {
    var totalNumberofRows = 5;
    var output="";
    for (var i = 1; i <= totalNumberofRows; i++) {
        for (var j = 1; j <= i; j++) {
            output+=j + "     ";
        }
        console.log(output);
        output="";
    }
}  
查看更多
登录 后发表回答