Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

194
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda