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

100
Vistas
Can't make pyramid of numbers

I Need to get this output:

1

2  3

4  5  6

7  8  9  10

11 12 13 14 15

What I've tried so far:

function getPyramid() {
  let nums = 0;
  for (let i = 1; i <= 15; i++) {
    for (let n = 1; n < i; n++) {
      console.log(n);

    }
    console.log('<br/>');

  }
  return nums;
}
getPyramid();

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Apart from notes given by jnpdx in the comments, I'd add some:

  • for better convention between programmers we tend to name varible nested in for-loop: i, j
  • <br/> for HTML new line, In JS we do \n for that!
  • number++ is same number += 1
  • Conditional_Operator (expression)?true:false instead of if/else
    function getPyramid(Lines) {
    let number = 1; // the counter to display on each line of pyramid!
    for (let i = 1; i <= Lines; i++) {
        let str = '';//line to display
        for (let j = 1; j <= i; j++) {
           str += number++;//incrementing counter
           str += j!=i ? ' ' : ''; //to make space, but not at the end of line.
        }
       console.log(str);//display that line
    }
    }
    getPyramid(5);
about 4 years ago · Juan Pablo Isaza Denunciar

0

    for (let i = 1 ; i <= 5; i++)
    {
      let s = []
      for (let x = i * (i - 1) / 2 + 1; x <= i * (i + 1) / 2; x++)
      { 
        s.push(x)
      }
      console.log(s.join(" "))
    }

about 4 years ago · Juan Pablo Isaza Denunciar

0

It is possible to do with one single loop

function getPyramid() {
  
let nums = 0;
let count = 1; 
let numbers = ''
  for (let i = 0; i <= 15; i++) {
if(count === nums){
     count++
     nums = 0;
     console.log(numbers)
     numbers = ''
 }
 nums ++
 numbers += ' ' + (i +1) 
  }

}
getPyramid();
Or like this you can specify amount of rows..
function getPyramid(rows) {
  
let nums = 0;
let count = 1; 
let numbers = ''
let i = 1
  while (count < rows + 1 ) {
if(count === nums){
     count++
     nums = 0;
     console.log(numbers)
     numbers = ''
 }
 nums ++
 numbers += ' ' + i
 i++;
  }

}
getPyramid(5);

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