1

I want to create a pyramid number structure. Can anyone explain me how?

for (i = 1; i <= 5; i++) {
  var k = ' ';
  var myspace = '';
  for (j = 0; j < i - 0; j++) {
    k += i;
    myspace += ' ';
  }
  console.log(myspace + k);
}

I want make it like this:

    1
   2 2
  3 3 3
 4 4 4 4
0

2 Answers 2

3

Try below code:

function createPyramid(rows) {
  for (var i = 0; i < rows; i++) {
    var output = '';
    for (var j = 0; j < rows - i; j++) output += ' ';
    for (var k = 0; k <= i; k++) output += (i + 1) + ' ';
    console.log(output);
  }
}

createPyramid(4);

Sign up to request clarification or add additional context in comments.

Comments

0

Try below method.

var a;
var n = 5;
for (var i = 1; i <= n; i++) {
  for (var j = 1; j <= i; j++) {
    document.write(j + " ");
  }
  document.write("<br />");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.