Jump to content

My codes an abortion :(


zartzar

Recommended Posts

Im supposed to create an X by X table using a for loop where X is a random number. Ive been racking my brain all day. I ask you with tears in my eyes, whats wrong! My logic at least seems correct to me..

 

$r(10,30);

 

for ($a=1; $a<=$r; $a++)

 

{print"<table><tr>";

 

for ($b=1; $b<=$r; $b++) {print "<td>x</td>";}

 

print "</tr>";

Link to comment
Share on other sites

Use variable names that actually mean something.  I can't understand your code because you've omitted important details like where you get the random value.

 

Off the top of my head, the first issue is that you have the

statement inside the loop.

 

You only want one table, so you should emit that table tag first.  Then you need 2 loops

 

Outer ( 1 - $x Will print the

tag)

 

Inner (1 - $x will print

)

 

After the inner for loop, you print the end

tag.

 

 

x
Link to comment
Share on other sites

Use variable names that actually mean something.  I can't understand your code because you've omitted important details like where you get the random value.

 

Off the top of my head, the first issue is that you have the <table> statement inside the loop.

 

You only want one table, so you should emit that table tag first.  Then you need 2 loops

 

Outer ( 1 - $x Will print the <tr> tag)

 

Inner (1 - $x will print <td>x</td>)

 

After the inner for loop, you print the end </tr> tag.

 

Sorry, im a noob.  Anyway, the random value is just that, a random value, it just determines how many rows and collums the table has. a and b are just counters. Or did i misunderstand you?

Link to comment
Share on other sites

You have this line of code:

 

$r(10,30);

 

That doesn't mean anything out of context.  If you had this:

 

$r = rand(10, 30);

 

Then it might make sense.  Anyways, I've given you the solution to your problems and identified what is going wrong for you.  Do you understand the answer I provided?

Link to comment
Share on other sites

Oh yeah, I guess I should say that your code is basically correct, but you are making a table every loop which is of course the main problem.  Only make the table once.  Otherwise, your code does what I described.

 

Thanks dude! Got it! Besides the table i didnt close the first loop properly. Works beautifully now :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.