for label and design output

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Nov 2008
Posts: 5
Reputation: zerogee2 is an unknown quantity at this point 
Solved Threads: 0
zerogee2 zerogee2 is offline Offline
Newbie Poster

for label and design output

 
0
  #1
Aug 29th, 2009
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.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. nextRow: // target label of continue statement
  2. for ( var row = 1; row <= 10; ++row )
  3. {
  4. document.writeln( "<br />" );
  5.  
  6. for ( var column = 10; column >= 1; --column )
  7. {
  8. if ( column < row )
  9. continue nextRow; // next iteration of labeled loop
  10.  
  11. document.write( "* " );
  12. } //end for
  13. } //end for
  14.  
  15. nextRow: // target label of continue statement
  16. for ( var row = 1; row <= 10; ++row )
  17. {
  18. document.writeln( "<br />" );
  19.  
  20. for ( var column = -1; column <= -10; ++column )
  21. {
  22.  
  23. if ( column < row )
  24. continue nextRow; // next iteration of labeled loop
  25.  
  26. document.write( "* " );
  27. } //end for
  28. } //end for
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 858
Reputation: Airshow is on a distinguished road 
Solved Threads: 122
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark

Re: for label and design output

 
0
  #2
Aug 29th, 2009
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 :
  1. function writeAsterisks(x, y){
  2. var i, j, n, s;
  3. for( i=0,n=x; i<(1+Math.abs(y-x)); i++, n+=(x<y)?1:-1 ) {
  4. s = [];
  5. for( j=0; j<n; j++ ) { s.push('*'); }
  6. document.writeln( s.join(' ')+ '<br />' );
  7. delete s;
  8. }
  9. }
  10. writeAsterisks(10, 1);
  11. writeAsterisks(1, 10);
Airshow
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: zerogee2 is an unknown quantity at this point 
Solved Threads: 0
zerogee2 zerogee2 is offline Offline
Newbie Poster

Re: for label and design output

 
0
  #3
Aug 29th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 858
Reputation: Airshow is on a distinguished road 
Solved Threads: 122
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark

Re: for label and design output

 
0
  #4
Aug 29th, 2009
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
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 858
Reputation: Airshow is on a distinguished road 
Solved Threads: 122
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark

Re: for label and design output

 
0
  #5
Aug 29th, 2009
Zero-G,

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

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function writeAsterisks(x, y, pad){
  2. var i, j, n, s;
  3. document.writeln( '<pre>' );
  4. for( i=0,n=x; i<(1+Math.abs(y-x)); i++, n+=(x<y)?1:-1 ) {
  5. s = [];
  6. if(pad){
  7. for(j=0; j<1+Math.abs(y-x)-n; j++){ s.push(' '); }
  8. }
  9. for( j=0; j<n; j++ ) { s.push('*'); }
  10. document.writeln( s.join(' ') );
  11. delete s;
  12. }
  13. document.writeln( '</pre>' );
  14. }
  15. writeAsterisks(1, 10, false);
  16. writeAsterisks(10, 1, false);
  17. writeAsterisks(10, 1, true);
  18. writeAsterisks(1, 10, true);
Use of <pre>...</pre> is good advice.

Airshow
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: zerogee2 is an unknown quantity at this point 
Solved Threads: 0
zerogee2 zerogee2 is offline Offline
Newbie Poster

Re: for label and design output

 
0
  #6
Aug 29th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 858
Reputation: Airshow is on a distinguished road 
Solved Threads: 122
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark

Re: for label and design output

 
0
  #7
Aug 29th, 2009
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
Last edited by Airshow; Aug 29th, 2009 at 10:55 pm.
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: zerogee2 is an unknown quantity at this point 
Solved Threads: 0
zerogee2 zerogee2 is offline Offline
Newbie Poster

Re: for label and design output

 
0
  #8
Aug 29th, 2009
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.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?xml version = "1.0" encoding = "utf-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4.  
  5. <html xmlns = "http://www.w3.org/1999/xhtml">
  6. <head>
  7. <title>Using the continue Statement with a Label</title>
  8. <script type = "text/javascript">
  9. <!--
  10. nextRow: // target label of continue statement
  11.  
  12. for ( var row = 1; row <= 10; ++row )
  13. {
  14. document.writeln( "<br />" );
  15.  
  16. for ( var column = 1; column <= 10; ++column )
  17. {
  18. if ( column > row )
  19. continue nextRow; // next iteration of labeled loop
  20.  
  21. document.write( "*" );
  22. } //end for
  23. } //end for
  24.  
  25. document.writeln( "<br />" );
  26.  
  27.  
  28. nextRow2: // target label of continue statement
  29. for ( var row = 1; row <= 10; ++row )
  30. {
  31. document.writeln( "<br />" );
  32.  
  33. for ( var column = 10; column >= 1; --column )
  34. {
  35. if ( column < row )
  36. continue nextRow2; // next iteration of labeled loop
  37.  
  38. document.write( "*" );
  39. } //end for
  40. } //end for
  41.  
  42. document.writeln( "<br />" );
  43.  
  44.  
  45. nextRow3: // target label of continue statement
  46.  
  47. for ( var row = 1; row <= 10; ++row )
  48. {
  49. document.writeln( "<br />" );
  50.  
  51. for ( var column = -10; column <= 1; ++column )
  52. {
  53.  
  54. if ( column > row )
  55. continue nextRow3; // next iteration of labeled loop
  56.  
  57. document.write( "*" );
  58.  
  59. } //end for
  60. } //end for
  61.  
  62. // -->
  63. </script>
  64. </head><body></body>
  65. </html>
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: zerogee2 is an unknown quantity at this point 
Solved Threads: 0
zerogee2 zerogee2 is offline Offline
Newbie Poster

Re: for label and design output

 
0
  #9
Aug 29th, 2009
oh, also i'm going to mark this post solved so you get the credit for helping.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 858
Reputation: Airshow is on a distinguished road 
Solved Threads: 122
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark

Re: for label and design output

 
0
  #10
Aug 30th, 2009
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
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC