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 03-02-2012, 06:28 AM   PM User | #1
weismana81
New to the CF scene

 
Join Date: Mar 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
weismana81 is an unknown quantity at this point
Can I increment in a for in loop?

I'm super stuck. Is it possible to increment in a for...in loop? This is what I'm trying to do, but the increments aren't happening. Any help or advice is much appreciated!!


Code:
var i_1 = 3;
var i_2 = 8;
		
for (var key in data.id) {

	if ([key] < '4')
	{
		attach_to = '.active';
	}
	else if ([key] > i_1 && [key] < i_2)
	{
		attach_to = '.item_' + i_1;

	}
	
	document.write(attach_to + '<br/>' + i_1+ '<br/>');
	i_1 + 4;
	i_2 + 4; 
}
weismana81 is offline   Reply With Quote
Old 03-02-2012, 06:47 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Increment what?

You mean here:
Code:
	i_1 + 4;
	i_2 + 4;
There is no incrementing being done by those statements. You add 4 to i_1 and then throw it on the floor. And do the same with i_2.

Perhaps you meant to do
Code:
	i_1 += 4;
	i_2 += 4;
THOSE statements will indeed increment each of those variables by 4.

But this code makes no sense either:
Code:
    if ([key] < '4')
(1) The syntax is illegal. You are using square brackets in an inaprorpriate place.
(2) You are comparing something (not sure what, because the square brackets are illegal) to a *STRING* '4'. Are you aware that the string '1000000' is *LESS THANT the string '4'? If you want to compare NUMERIC values, use numbers, not strings.

Finally, I don't know when or where this code fragment is supposed to run, but if it runs at any point in time *AFTER* the HTML page is finished loading, it will WIPE OUT ALL CONTENTS in the page, including even the code you show here. You can't use document.write after a page is loaded.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 03-02-2012, 07:13 AM   PM User | #3
weismana81
New to the CF scene

 
Join Date: Mar 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
weismana81 is an unknown quantity at this point
Thanks so much for the guidance!

I didn't mean to be vague with the code. This is part of a much bigger project and I was just trying to break it down the specific problem I was having. I was just using document.write to help me visualize the output.

Silly mistake with the using strings instead of numbers :/ I've been using a lot of numbers with css to control behaviors on the page and I just... yeah...

Anyway, I really appreciate your help!! += was definitely what I was looking for and you no doubt saved me a silly amount of time catching that 'number' thing. Thanks!
weismana81 is offline   Reply With Quote
Reply

Bookmarks

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 11:51 PM.


Advertisement
Log in to turn off these ads.