ok,
i wrote a label for statement that on row 1 has "*" and nine spaces.
row 2 has 2 "*" and 8 spaces and so on...... till my tenth row has ten "8"

now i need to do the opposite;
row 1; 9 spaces and 1 *
row 2; 8 spaces and 2 * and so on......
row 10 has 10 *


here is what i have so far. the first nextRow Label is good but the second one isn't.

nextRow: // target label of continue statement
            for ( var row = 1; row <= 10; ++row )
            {
               document.writeln( "<br />" );

               for ( var column = 10; column >= 1; --column )
               {
                  if ( column < row )
                     continue nextRow; // next iteration of labeled loop   
   
                  document.write( "* " );
               } //end for
            } //end for

        nextRow: // target label of continue statement
            for ( var row = 1; row <= 10; ++row )
            {
               document.writeln( "<br />" );
                               
               for ( var column = -1; column <= -10; ++column )
               {
                       
                       if ( column < row )
                         continue nextRow; // next iteration of labeled loop   
                   
                  document.write( "* " );
               } //end for
            } //end for

Recommended Answers

All 10 Replies

Zero-G,

It's all to do with column < row always being false.

But you should really be thinking in terms of a generalised function to do this sort of thing, with parameters to control the detailed behaviour.

For example :

function writeAsterisks(x, y){
	var i, j, n, s;
	for( i=0,n=x; i<(1+Math.abs(y-x)); i++, n+=(x<y)?1:-1 ) {
		s = [];
		for( j=0; j<n; j++ ) { s.push('*'); }
		document.writeln( s.join(' ')+ '<br />' );
		delete s;
	}
}
writeAsterisks(10, 1);
writeAsterisks(1, 10);

Airshow

Unfortuanly i cannot do the code that way. I need to use for statements with labels. see assignment;

Write an script that outputs xhtml to display the following patterns separately, one below the other. Use for statements to generate the patterns. All asterisks (*) should be printed by a single statement of the form document.write ( '*' ); which causes the asterisks to print side by side. A statement of the form document.write("<br />"); can be used to move to the next line. A statement of the form document.write( " " ); can be used to display a space for the last two patterns. There should be no other output programs in the program. [Hint: The last two patterns require that each line begin with an appropriate number of blank spaces. you may need to use the xhtml <pre></pre> tags.]

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

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

**********
.*********
..********
...*******
....******
.....*****
......****
.......***
........**
.........*

.........*
........**
.......***
......****
.....*****
....******
...*******
..********
.*********
**********

I have gotten the first two patterns but the last two elude me. oH, of note about this post and the last two patterns. the right border should line up. for some reason it's not posting right.

Aha, so that's what an assignment looks like.

Well with the greatest respect to your lecturer, it's abit out of date in the direction it steers you. In particular, these days nobody should be encouraged to use document.write(); , which is one of the greatest evils ever include in Javascript. I used it for many years before I saw the light.

Personally, I would regard much of the assigment as advice.

Symantically, only the first two sensences are instructions. The rest have main verbs "should" and "can" and may therefore be regarded as advisory not mandatory. In English, mandation is expressed by use of the imperitive tense (as in sentences 1 and 2), or main verbs "must" or "shall".

That said, I don't think there's anything in my code which departs from the assigment - even including the advisory stuff. Here's my statement of compliance (it's a useful exercise) :

  1. Write an script that outputs xhtml to display the following patterns separately
    Compliant
  2. one below the other.
    Compliant
  3. Use for statements to generate the patterns.
    Compliant
  4. All asterisks (*) should be printed by a single statement of the form document.write ( '*' ); which causes the asterisks to print side by side.
    (Advisory) Compliant
  5. A statement of the form document.write("<br />"); can be used to move to the next line.
    (Advisory) Compliant
  6. A statement of the form document.write( " " ); can be used to display a space for the last two patterns.
    (Advisory) Compliant
  7. There should be no other output programs in the program.
    (Advisory) Compliant
  8. [Hint: The last two patterns require that each line begin with an appropriate number of blank spaces. you may need to use the xhtml <pre></pre> tags.]
    (Advisory) Compliant

For several years I wrote System Requirements Specifications in which every statement was very carefully measured to convey precisely the right level of meaning/mandation/advise. "Should" was generally avoided (unless specifically defined) because it can imply mandation when none was intended and vice versa, leaving the respondant (you in this case) free to apply the most favourable interpretation - each statement individually. That gives you a lot of freedom to code the thing how you want.

So if they argue, tell them Airshow said so.

Airshow

Zero-G,

I guess you will want to do it your way but this will give you a clue:

function writeAsterisks(x, y, pad){
	var i, j, n, s;
	document.writeln( '<pre>' );
	for( i=0,n=x; i<(1+Math.abs(y-x)); i++, n+=(x<y)?1:-1 ) {
		s = [];
		if(pad){
			for(j=0; j<1+Math.abs(y-x)-n; j++){ s.push(' '); }
		}
		for( j=0; j<n; j++ ) { s.push('*'); }
		document.writeln( s.join(' ') );
		delete s;
	}
	document.writeln( '</pre>' );
}
writeAsterisks(1, 10, false);
writeAsterisks(10, 1, false);
writeAsterisks(10, 1, true);
writeAsterisks(1, 10, true);

Use of <pre>...</pre> is good advice.

Airshow

Airshow,

thanks forthe advice. I guess i was just interping the directions defferntly. Thanks for showing me the error in my ways...hahahaha

I like your code but it looks very complicated to me. I am going to try and work it my way, but even with your "suggestions" i'm still not seeing it. I know the <pre> tag is the key but when ever i put the tag in the code the *'s align vertically only.

i have been working on this for days with little progress. But again i appericate the advice. if you have more "prompts" feel free to give them.

thx
zero

Zero-G,

I'm glad you appreciate my essay on bad assignment wording. Truth is there's a bit of a rebel in me still.

The second set of *** is actually easier than the first.

Every row has 10 entries. Each entry is either " " or "* ". So, doing it your way, the inner (column) loop always goes round 10 times - no need for a break.

Within the loop, all you have to do is decide each time whether to print out " " or "* ", which depends on the outer (row) loop counter. The rule is only slightly different for increasing versus decreasing.

Make sure everything is wrapped in <pre></pre> otherwise the browser will render consecutive spaces as a single space, and (in all probability) it will use a propertional font such that spaces and asterisks are different widths. You will only ever get that nice straight right edge with a "mono-spaced" font, as per <pre>.

I am determined you're going to get this right and get a good mark.

Airshow

when i wrap everything with the pre tags it doesn't show anything, not even the patterns i had already. I know the decreassing the row pring is the "--column". this is my complete code, obviously the last label is the one i'm working on.

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml">
   <head>
      <title>Using the continue Statement with a Label</title>
      <script type = "text/javascript">
         <!--
         nextRow: // target label of continue statement
         
            for ( var row = 1; row <= 10; ++row )
            {
               document.writeln( "<br />" );

               for ( var column = 1; column <= 10; ++column )
               {
                  if ( column > row )
                     continue nextRow; // next iteration of labeled loop   
   
                  document.write( "*" );
               } //end for
            } //end for
            
            document.writeln( "<br />" );

            
         nextRow2: // target label of continue statement
            for ( var row = 1; row <= 10; ++row )
            {
               document.writeln( "<br />" );

               for ( var column = 10; column >= 1; --column )
               {
                  if ( column < row )
                     continue nextRow2; // next iteration of labeled loop   
   
                  document.write( "*" );
               } //end for
            } //end for
            
            document.writeln( "<br />" );

		
		nextRow3: // target label of continue statement
			
            for ( var row = 1; row <= 10; ++row )
            {
               document.writeln( "<br />" );
               				
               for ( var column = -10; column <= 1; ++column )
               {
               		
               		if ( column > row )
                  	   continue nextRow3; // next iteration of labeled loop   
   				   
                  document.write( "*" );
                  
               } //end for
            } //end for
			
         // -->
      </script>
   </head><body></body>
</html>

oh, also i'm going to mark this post solved so you get the credit for helping.

Zero-G,

I'm away for a couple of days now so may go a bit quiet. There's a computer where I'm going but I'm not sure I will find the time.

Airshow

I can't explain why <pre> should make everything disappear. Very odd. You must get pre working. (There is another way but it involves CSS which you may not have done yet).

In patterns 3 and 4, try looping column from 1 to 10 (same as for row). Then inside the inner loop, you need lines of the form:

if( [I]expression[/I] ) { write(" "); }
else { write("*"); }

You just need to derive expression. You are nearly correct in your code above in that it involves both inner and outer loop counters.

Get rid of the continue statement (and the label) because for patterns 3 and 4 you want to loop through all 10 column iterations on every row - guaranteed.

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.