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

168
Visualizações
Document.write("*"); for loop returns an undefined value at the end of my "*" pyramid?

I am building a simple for-loop function that returns a pyramid of stars from number input. The problem is it keeps returning an "undefined" value at the end of the pyramid... here is the code:

function ligne(x)
{
    for(i = 0; i < x; i++)
    {
        for(j = 0; j <= i; j++)
        {
            document.write("*");
        }
        document.write("<br/>");
    }
};

var num = prompt("Entré un nombre.");
var pyramid = ligne(num);
document.write(pyramid);

And here is the result on the web page with an input of 8:

*
**
***
****
*****
******
*******
********
undefined 

I think the problem is eiter with the "document.write()" statement in the last for-loop or at the end of the code. The reason for that is because when I remove the line break in the first for-loop, the stars print out in one line with the "undefined" value still at the end.

Anyone could help??

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

'document.write(pyramid); this prints you undefined because your function doesn't return anything. if you delete it or return something to the function it will be fixed.

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