JavaScript Pyramide of Stars Javascript

How could I create Pyramide of Stars that increase every row by 2 like that:

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

My currently code:

for (var x = 0; x < 5; x++) {
    for (var y = 0; y <= x; y = y + 1) {
        document.write(" * ");
    }
    document.write("<br>");
}
Answer:1

It's possible just to increment in your loop by 2.

for(var i = 1; i < 20; i += 2) {
    console.log( Array(i).fill('*').join(' ') );
}

Otherwise just multiply inside your loop

for(var i = 0; i < 10; i++) {
    console.log( Array(i*2 + 1).fill('*').join(' ') );
}

You may also need to polyfill Array.fill depending on your target.

Answer:2

Other answers recreate the entire row each time. This solution just extends the row each time to have another star.

function pyramid(n) {
  let result = '', str = '', add = '*';
  for (var i = 0; i < n; i++) {
    str += add;
    add = ' *';
    if (!(i % 2)) result += str + '\n';
  }
  return result;
}

console.log(pyramid(5));
Answer:3

You can do like this.

function generate() {
    var totalNumberofRows = 5;
    var output="";
    for (var i = 1; i <= totalNumberofRows; i++) {
      for (var j = 1; j <= i; j++) {
        if(j==1)
          output+="*";
        else
          output+="     "+ "*" + "     "+ "*";
  }
  console.log(output);
      output="";
   }
}  
generate()
Answer:4

This is driving me absolutely insane, and I just cannot see what I have done wrong. Please help before I start bibbling and gnawing my colleagues leg off. He doesn't deserve that. I have an object I ...

This is driving me absolutely insane, and I just cannot see what I have done wrong. Please help before I start bibbling and gnawing my colleagues leg off. He doesn't deserve that. I have an object I ...

I currently have a table and each cell has a button. Upon clicking the button, based on that particular day (Monday or Tuesday), class (class 1 or class 2), and name (Kev or Josh), how can I push an ...

I currently have a table and each cell has a button. Upon clicking the button, based on that particular day (Monday or Tuesday), class (class 1 or class 2), and name (Kev or Josh), how can I push an ...

Lets say I had a HTML5/JS based application that is wrapped in cordova phone gap. Can I have it startup and download additional html5/js files? Basically the app would work with the base files, but ...

Lets say I had a HTML5/JS based application that is wrapped in cordova phone gap. Can I have it startup and download additional html5/js files? Basically the app would work with the base files, but ...

I have a form with multiple TextBoxes, Textarea and Buttons. By pressing the ENTER in any text box submits the form. So i want to disable the pressing enter only in text boxes and to enable in text-...

I have a form with multiple TextBoxes, Textarea and Buttons. By pressing the ENTER in any text box submits the form. So i want to disable the pressing enter only in text boxes and to enable in text-...

  1. disable enter submit form
  2. disable enter submit form angular
  3. disable enter submit form javascript
  4. disable enter submit form jquery
  5. disable enter submit
  6. disable enter submit form html
  7. disable enter submit jquery
  8. disable enter submit form react
  9. disable enter submit form asp.net
  10. disable enter submit angular
  11. disable submit enter key
  12. html disable enter submit
  13. disable enter key submit form
  14. input disable enter submit
  15. js disable enter submit
  16. formik disable enter submit
  17. javascript disable enter submit
  18. disable press enter submit form
  19. rails disable enter submit
  20. mvc disable enter submit