Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
How to create multiple pyramid in Javascript?

I want to create a multiple pyramid program using Javascript but I really don't understand from where should I start to create multiple star pyramid program.

Note: I have to create this using For Loop.

Output I want:

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

Here is my Code:

    for(i=1;i<=3;i++){
        for(j=1;j<=i;j++){
            document.write("*"+ " ")
        }
        for(k=5;k>=1;k=k-2){
          for(m=1;m<=k;m++){
              document.write("*")
          }
 
        }
    
        document.write("<br>")
    } 
  

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

The following code would draw a double pyramid of N levels, using underscores (_) to depict spaces. You may modify the characters used.

const drawPyramid = length => {
  for (let i = 0; i < length; i++) {
    
    const N = 2*(length - i - 1)
    , blankValues = [...Array(N+1).keys()].slice(1).map(n => n + i)
    
    for (let j = 0; j < 2 * length; j++) {
        if (!blankValues.includes(j)) document.write("* ")
        else document.write("_ ")        
    }
    document.write("<br>")
  }
}

drawPyramid(3)
document.write("<br>")
drawPyramid(4)
document.write("<br>")
drawPyramid(10)

about 4 years ago · Juan Pablo Isaza Report

0

Try the following:

var allowedIndexes = [];
const columnNum = 6
for(var i=0; i<3 ;i++){
    
    allowedIndexes.push(i)
    allowedIndexes.push(columnNum - (i+1))
    for(var j=0;j< columnNum ;j++){
            
        if(allowedIndexes.includes(j)){
            document.write("*&nbsp;")
        }
        else {
            document.write("&nbsp;&nbsp;&nbsp;")
        }

    }

    document.write("<br>")

}
  

about 4 years ago · Juan Pablo Isaza Report

0

const writePyramid = (levels, row = 0, level = [], spacers = 0) => {
    if (row === levels) return;
    document.write("<br>")
    if (spacers === 0) spacers = levels + 1;
  
    let stars = new Array(row + 1).fill('*')
  
    level = [...stars, ...new Array(spacers > 1 ? spacers : 0).fill('&nbsp;&nbsp;'), ...stars]

    document.write(level.join(''))
    
    writePyramid(levels, row + 1, level, spacers / 2);
}

writePyramid(3)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!