/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] write a script

I need to write a script that outputs XHTML to display the following patterns:

a)

*
**


***

****

*****

******

*******

********

*********

**********

b)


**********

s *********
s s*
**
*****
s s s*
**
****
s s s s*
**
***
s s s s s*
**
**
s s s s s s**
**
s s s s s s s**
*
s s s s s s s s**
s s s s s s s s s*

s= blank spaces

the 2nd pattern starts with 10 asterisks, then 1 blank space and 9 asterisks, 2 blank spaces and 8 asterisks, 3 blank spaces and 7 asterisks…9 blank spaces and 1 asterisks

All asterisks (*) should be printed by a single statement of the form document.write( “*” ); (this causes the asterisks to print side by side). A statement of the form document.writeln( “<br />” ); can be used to position to the next line. A statement of the form document.write( ” ” ); can be used to display a space (needed for the second pattern). There should be no other output statements in the program. The last pattern requires that each line begin with an appropriate number of blanks. I guess I may need to use the XHTML <pre> </pre> tags.

I was able to do the first pattern but I’m stuck with the second one. Anyone can help? Thanks

This is my script for the first pattern:
<!–
nextRow:
for ( var row = 1; row <= 10; ++row) {
document.writeln( “<br />” );

for ( var column = 10; column <= 10; –column) {
if ( column < row )
continue nextRow;

document.write( “* ” );
}
}

//–>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@jalarieFeb 21.2007 — I usually do such homework problems by breaking the "spaces" part and the "asterisks" part into two separate loops:
<i>
</i> &lt;script type="text/javascript"&gt;
&lt;!-- Hide this code from non-JavaScript browsers
document.write('&lt;pre&gt;');
for (i1=1; i1&lt;=10; i1++) {
for (i2=1; i2&lt;=i1; i2++) {
document.write('*');
}
document.writeln('');
}
document.writeln('');
for (i1=1; i1&lt;=10; i1++) {
for (i2=2; i2&lt;=i1; i2++) {
document.write(' ');
}
for (i2=i1; i2&lt;=10; i2++) {
document.write('*');
}
document.writeln('');
}
document.write('&lt;/pre&gt;');
// End hiding --&gt;
&lt;/script&gt;
Copy linkTweet thisAlerts:
@RickLVauthorFeb 21.2007 — you made my day!

Thanks so much.

Rick
×

Success!

Help @RickLV spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...