Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

95
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda