Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

nested for structures

Status
Not open for further replies.

MarkJ2

Technical User
Sep 25, 2001
17
US
I'd like to write a script that will read in an odd number to specify the number of rows of a diamond. If the user enters 5 the diamond will look like so:

*
***
*****
***
*

I know I will need nested for structures and output statements that print a single asterik, a single space or a single newline character, but I can't figure out the code.
The code for the initial prompt I understand.

Any help would be appreciated.
 
this might help get you started:

Code:
<DIV align=&quot;center&quot;>
<SCRIPT>
var lines=7;
var half=(lines+1)/2;
var stars = 0;
for (i=1; i<=half; i++) { 
    stars++;
    for (j=1; j<=stars; j++) {
        document.write(&quot;*&quot;);
    }
    document.write(&quot;<BR>&quot;);
    
}
for (i=half; i<=lines; i++) { 
    stars--;
    for (j=1; j<=stars; j++) {
        document.write(&quot;*&quot;);
    }
    document.write(&quot;<BR>&quot;);
    
}
</SCRIPT>
</DIV>

the variable &quot;lines&quot; is the one you'd want to set based on user input. note i've made it look like a diamond by putting it in a div aligned center, you could do the same in a table cell with central alignment, or you'll have to modify it to write spaces as well as *'s

hope that helps...

~ ~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top