Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (7k points)

I want to print some patterns with stars, can someone help me with this?

1 Answer

0 votes
by (13.1k points)

You can do it like this:

for (let i = 1; i <= 5; i++) {

    for (let j = 1; j <= i; j++) {

        document.write('*');

    }

    document.write('<br />');

}

If you want a pyramid:

function pyramid(n) {

    for(i=1 ;i<=n;i++) {

        let str = ' '.repeat(n-i);

        let str2 = '*'.repeat(i*2-1);

    console.log(str + str2 + str);

    }

}

pyramid(5)

Want to be web developer? Check out the full stack developer course from Intellipaat. 

Browse Categories

...