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

192
Views
JavaScript - Right Pascal pattern with the Fibonacci sequence

I'm trying to create a Pascal triangle using the Fibonacci sequence. I´m looking for this output:

0
0 1
0 1 1
0 1 1 2
0 1 1 2 3
0 1 1 2 3 5
0 1 1 2 3 5 8 
0 1 1 2 3 5
0 1 1 2 3
0 1 1 2
0 1 1
0 1
0

This is the code I have written so far. I managed to get the Fibonacci sequence running into a triangle but not the way I want.

function fiboP(n) {
  let string = "";
  let n1 = 0
  let n2 = 1
  for (let i = 1; i <= n; i++) {
    for (let j = 0; j < i; j++) {
      string += n1 + " ";
      next_num = n1 + n2;
      n1 = n2;
      n2 = next_num;
    }
    string += "\n";
  }
  for (let i = 1; i <= n - 1; i++) {
    for (let j = 0; j < n - i; j++) {
      string += n1 + " ";
      next_num = n2 - n1;
      n2 = n1;
      n1 = next_num;
    }
    string += "\n";

  }
  console.log(string)
}


fiboP(5)

Output:
0 
1 1 
2 3 5 
8 13 21 34 
55 89 144 233 377 
610 377 233 144 
89 55 34 
21 13 
8

I would like to understand what I am missing here and if there is a cleaner and simpler way to produce the desired output.

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

0

If you reset your values when you go to next line, you should be able to generate the output you're looking for

function fiboP(n) {
  let string = "";
  for (let i = 1; i <= n; i++) {
    let n1 = 0
    let n2 = 1
    for (let j = 0; j < i; j++) {
      string += n1 + " ";
      next_num = n1 + n2;
      n1 = n2;
      n2 = next_num;
    }
    string += "\n";
  }
  for (let i = 1; i <= n - 1; i++) {
    let n1 = 0
    let n2 = 1
    for (let j = 0; j < n - i; j++) {
      string += n1 + " ";
      next_num = n2 + n1;
      n2 = n1;
      n1 = next_num;
    }
    string += "\n";

  }
  console.log(string)
}


fiboP(7)

As an improvement i will suggest finding fibonacci sequence once and then just using these values to create this triangle.

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!