Click here to Skip to main content
15,895,192 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi! So i have been trying to create the pattern below using nested for or while loops, however my code does not seem to put out what I need. In fact, It displays nothing!

Pattern:
*
**
***
****
*****
******
*******
******
*****
****
***
**
*

Below is the code I have been trying out.

What I have tried:

for (var count = 1; count < 8; count+= 1;) {
document.write(count + "*
");

for (var count2 = 1; count < 8; count -= 1;) {
document.write(count + "*
");
}
}
Posted
Updated 5-Jul-18 5:45am
Comments
F-ES Sitecore 5-Jul-18 10:51am    
The syntax of your "for" loop is wrong, consult the documentation on how to do a javascript for loop, or google it. You really need to improve your basic understanding of javascript before you attempt any actual work. We can't teach you a whole language from scratch via forum posts.
Patrice T 5-Jul-18 11:02am    
post a code that we can run.
Patrice T 5-Jul-18 11:11am    
you should reread careffuly your code, it is plain wrong.

1 solution

You could do something like this:

for (var count = 1; count <= 7; count++)
{
    for(var i = 0;i<count;i++)
    {
        document.write("*");
    }
    document.write("<br>");
}

for (var count = 6; count >=1 ; count--)
{
    for(var i = 0;i<count;i++)
    {
        document.write("*");
    }
    document.write("<br>");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900