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

304
Views
JavaScript pyramid

I'm trying to print a pyramid that has odd numbers for edges. For example output should be:
.......5
...3,4,3
1,2,3,2,1
(dots are here just to show formating)

I managed to write:

function p(n){
  let string = "";

for (let i = 1; i <= n; i++) {
 
  for (let j = 1; j <= n - i; j++) {
   string += " " ;
  }
  
  for (let k = 1; k <= 2*i-1; k++) {
    string += n-k +1;
  }
 string += "\n";
}
console.log(string);
  
}


p(5);

But I'm not sure how to continue to get wanted result

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

0

You can try it :

function p(n){
  let string = ""
  for (let i = 0; i <= n/2; i++) {
    let k = n - 2 * i
    for (let j =0; j < Math.floor(n/2)*2 + 1; j++) {
      if(j < Math.floor(n/2) - i) {
        string += " ";
      }
      if( j >= Math.floor(n/2) - i && j <= Math.floor(n/2) + i) {
        string += k;
        j < Math.floor(n/2) ? k++ : k--
      }
      if(j > Math.floor(n/2) + i ) {
        string += " ";
      }
    }
    string += "\n";
  }
  console.log(string);
}

p(7);

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!