Home > Back-end >  Document.write("*"); for loop returns an undefined value at the end of my "*" py
Document.write("*"); for loop returns an undefined value at the end of my "*" py

Time:10-29

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??

CodePudding user response:

'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.

  • Related