Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-15-2012, 05:03 PM   PM User | #1
eyerate
New to the CF scene

 
Join Date: Oct 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
eyerate is an unknown quantity at this point
Trying to line up my output

Hello every body,

Trying to format my out put numbers. I know how in java but i guess JavaScript is different.

Example:

I want
Code:
                
1  3  5  7  9      
11 13 15 17 19      
21 23 25 27 29
I have
1 3 5 7 9
11 13 15 17 19
21 23 25 27 29


Here is my Code
Code:
<SCRIPT LANGUAGE="JavaScript">
		
		<!--
		
		for (var i = 1; i < 30; i++ )
		{
			if (i%2 == 1)
			{
				document.writeln(i);
				
					if (i==9)
					{
					document.write("<br>");		
					}

					if (i==19)
					{
					document.write("<br>");
					}
			}
		}

		document.write("<hr />");	
		//-->
		
		</SCRIPT>

Any help or hints most appreciated. Thanks for your Time!

EYERATE
eyerate is offline   Reply With Quote
Old 10-15-2012, 05:17 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Hints

1 - Don't use document.write (try modern DOM manipulation methods like .createElement, .appendChild, .insertBefore or even .innerHTML)
2 - Use a table with no border
Code:
<table border="0">
  <tbody>
    <tr>
      <td>First row, first column</td>
      <td>First row, second column</td>
      <td>...</td>
    </tr>
    <tr>
      <td>Second row, first column</td>
      <td>...</td>
    </tr>
    <tr>
      ...
    </tr>
  </tbody>
</table>
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
eyerate (10-15-2012)
Old 10-15-2012, 05:45 PM   PM User | #3
eyerate
New to the CF scene

 
Join Date: Oct 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
eyerate is an unknown quantity at this point
thanks for reply still not working for me, I could use tables but I'm trying to figure out how to justify output in java Script.
eyerate is offline   Reply With Quote
Old 10-15-2012, 07:37 PM   PM User | #4
eyerate
New to the CF scene

 
Join Date: Oct 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
eyerate is an unknown quantity at this point
OK I figured out away to do it if any on else has my problem. I used spaces between the single digit numbers. If their is a proper way to do it , I'm all ears but here is what I did.


Code:
for (var i = 1; i < 30; i++ )
		{
			if (i%2 == 1)
			{
				document.writeln(i + "&nbsp");

					if (i < 10)
					{
					document.write("&nbsp;&nbsp;");
					}
					if (i==9)
					{
					document.write("<br>");		
					}

					if (i==19)
					{
					document.write("<br>");
					}
			}
		}

		document.write("<hr />");
so pretty much just add spaces to any number (< 10)

Code:
if (i < 10)
{
document.write("&nbsp;&nbsp;");
}
eyerate is offline   Reply With Quote
Old 10-15-2012, 08:56 PM   PM User | #5
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
as has been noted, to do this "properly" you would probably create elements and align them using css, like this:
Code:
<head>
<style>
span{
width:30px;
float:left}
</style>
</head>
<body>

<script type="text/javascript">
for (var i = 1; i < 30; i++ ){
			if (i%2 == 1){
			var sp=document.createElement("span");
			sp.innerHTML=i+" ";
			document.body.appendChild(sp)
			if (i==9||i==19){
		document.body.appendChild(document.createElement("br"));
		}
	}
}
document.body.appendChild(document.createElement("br"));
document.body.appendChild(document.createElement("hr"));
</script>
</body>
but if you're just looking for quick and sleazy, but want to get away from document.write:
Code:
<script type="text/javascript">
for (var i = 1; i < 30; i++ ){
			if (i%2 == 1){
			if (i<10){
			document.body.innerHTML+=i+"&nbsp;&nbsp;&nbsp;"
			} 
			if (i>10){
			document.body.innerHTML+=i+"&nbsp;"
			} 
			if (i==9||i==19){
			document.body.innerHTML+="<br>"
		}
	}
}

document.body.innerHTML+="<hr>"
</script>
xelawho is offline   Reply With Quote
Reply

Bookmarks

Tags
line up output, number format, space problem

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:31 AM.


Advertisement
Log in to turn off these ads.