Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Even multiple 1

Status
Not open for further replies.

vasilek

Programmer
Jan 5, 2003
99
0
0
US
Hi. In the for loop how to find out if the current is an even multiple? Thanks.
 
put in an if...else statement thats something like this
Code:
if (i == 3 ) {
document.write(" The i varible is at 3")
}
and you can jus change the number to check a different number
hope that helps
Jammer1221
 
What I meant I guess is that I need to write some code evrytime can be evenly divided by 3 for example
 
% (Modulus)
The modulus operator is used as follows:

var1 % var2

The modulus operator returns the first operand modulo the second operand, that is, var1 modulo var2, in the preceding statement, where var1 and var2 are variables. The modulo function is the integer remainder of dividing var1 by var2. For example, 12 % 5 returns 2.

There's always a better way. The fun is trying to find it!
 
tviman, how do you think your post is related to my question? I need to write 5 rows an 3 columns, for example, if the search in the database returned 15 records (results).
 
>> "how do you think your post is related to my question"

like so:
Code:
if (myRowCounter % 3 == 0) {
  //  even multiple of three, so do something...
}

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
For some reason all fo the above do not work. When compared with an integer value it should return True if the current is an even multiple of the value it is being compared against.

If == 10 ... should repeat the contents of the If every 10th repetition.

Does it make sense to anyone?

 
vasilek was saying different needs in two different locations!

"Even multiple" :
if (myRowCount % 2 == 0){}
"Evenly divided by 3" (my English is under stress) :
if (myRowCount % 6 == 0){}

No wonder what proposed do not work!??

-tsuji
 
vasilek...

Maybe the easiest thing is to post some examples. I'm confused by your question (and then by some of the followups)... in that I do not know exactly what it is you want.

When you refer to I assume you mean the contents of the variable "i" (not the i-th index of the array called content). Maybe change the variable name to something with more meaning (suggest: "loop" as a more meaningful choice for this example).

The solution to your problem is going to involve the modulus operator... and it will be simple... but I'm unable to figure out what it is you want from the information you supplied.

Maybe provide an example that explains all the possibilities in advance (so we don't have to guess).

Cheers,
Jeff
 
Vasilek... the solution you need is in the use of the modulo symbol. Lets assume you have 15 records (and is 15), then i % 3 would return 0, meaning that there is no remainder of 15 divided by 3. If you test i % 3 each time the loop cycles, then each time the result of the modulo function returns 0, then you have an exact mutliple of 3.

There's always a better way. The fun is trying to find it!
 
Sorry everyone for the whole confusion. I thought of even multiple here but may be there is a better way. Here is my code:

Let's say that Categories.length = some number

document.write('<table>');
document.write('<tr>');
for (i=0; i<Categories.length; i++){
document.write('<td>');
document.write('<img src="image.jpg" width="80" height="100"><br>');
document.write('<a href="#">'+Categories+'</a><br>');
document.write('</td>');
}
document.write('</tr>');
document.write('</table>');


My code will write all <td>s on one <tr>, but I would like to have a table with only 3 columns, so that the next <td>s will be placed in the new <tr>

Is this clear more or less?
Thanks to everyone!!!
 
Regardless of what you do, you're going to have to write a new set of <tr></tr> tags for every set of 3 <td></td> tags. Your approach above is almost there, you just need to move, or add, another for loop before you write your <tr> tag.

There's always a better way. The fun is trying to find it!
 
I put the && (i != Categories.length - 1) in so that it wouldn't start a new row if it was at the end of the loop. Also, it won't close the last <tr>, but closing the table will take care of that for you.
Code:
document.write('<table>');
document.write('<tr>');
for (i=0; i<Categories.length; i++){
  document.write('<td>'); 
  document.write('<img src="image.jpg" width="80"
  height="100"><br>');
  document.write('<a href="#">'+Categories[i]+'</a><br>');
  document.write('</td>');
  if ((i % 3) == 2 && (i != Categories.length - 1)) {
    document.write('</tr>');
    document.write('<tr>');
  }
}
document.write('</table>');

-kaht

banghead.gif
 
hmm.... I guess you should add:
Code:
document.write('</tr>');
right before this:
Code:
document.write('</table>');
I took it out originally so you wouldn't get 2 </tr>'s in a row, but putting in the && (i != Catergories.length -1) will take care of that problem.

-kaht

banghead.gif
 
Thank you, kaht. The problem is solved!!!
 
Just a tip...

Instead of using a whole lot of document.write commands, append the string values to a variable and then write out that one variable once.

Example:
Code:
document.write('<p>');
document.write('Hello world!');
document.write('<p>');

Can become:
Code:
var _html = '<p>';
_html += 'Hello world!';
_html += '</p>';
document.write(_html);

My example is intended to show *how* to do this... not to suggest that this is the best solution for the example given.

I was thinking about all of those document.write statements being used in a for...loop (as in this thread) and how it would be better to append the data to a variable.

This is considered "better" by many coders (self included) because it requires less processing power (ie: runs faster). It's also important to know when to apply this technique (2 or 3 lines... no; 20 or 30 line... sure).

Jeff
 
actually, depending on your browser, document.write() or using arrays can be much more efficient than string concatenation.

test harness:
Code:
<html>
	<head>
		<title>test</title>
		<script type="text/javascript">
			//  string concatenation
			var start1 = new Date();
			document.write('<br/>string concatenation start');
			
			var s = "";
			for (var x = 0; x < 10000; x++)
				s += "<br/>line number " + x;
			
			document.write(s);
			var end1 = new Date();
			
			//  multiple document.write()
			var start2 = new Date();
			document.write('<br/>multiple document.write() start');
			
			for (var x = 0; x < 10000; x++) 
				document.write("<br/>line number " + x);
				
			var end2 = new Date();
			
			//  array
			var start3 = new Date();
			document.write('<br/>array start');
			
			var ar = [];
			for (var x = 0; x < 10000; x++)
				ar.push("<br/>line number " + x);
			
			document.write( ar.join("") );
			var end3 = new Date();
			
			//  totals
			var time1 = (end1 - start1) / 1000;
			var time2 = (end2 - start2) / 1000;
			var time3 = (end3 - start3) / 1000;
			
			document.write("<br/>string concatenation: " + time1 + " seconds");
			document.write("<br/>multiple document.write(): " + time2 + " seconds");
			document.write("<br/>array: " + time3 + " seconds");
		</script>

	</head>

	<body></body>
</html>

IE6 XP SP2
string concatenation: 10.084 seconds
multiple document.write(): 0.481 seconds
array: 2.704 seconds

firefox 0.8
string concatenation: 0.571 seconds
multiple document.write(): 60.486 seconds
array: 1.793 seconds

on P4 2.4 GHz, 256MB RAM, WinXP Pro

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top