OutFront Forums
     Home    Register     Search      Help      Login    

Sponsors
Frontpage website templates
Creative Website Templates for FrontPage, Dreamweaver, Flash, SwishMax
Shopping Cart Software
Ecommerce software integrated into Frontpage, Dreamweaver and Golive templates. No monthly fees and available in ASP and PHP versions.
Website Templates
We also have a wide selection of Dreamweaver, Expression Web and Frontpage templates as well as webmaster tools and CSS layouts.

Follow Us
On Facebook
On Twitter
RSS
Via Email

Recent Posts
Todays Posts
Most Active posts
Posts since last visit
My Recent Posts
Mark posts read

 

Javascript help

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
Users viewing this topic: none
Printable Version 

All Forums >> Web Development >> General Web Development >> Javascript help
Page: [1]
 
 
Starhugger

 

Posts: 661
Joined: 4/12/2005
From: Canada
Status: offline

 
Javascript help - 2/28/2009 16:53:33   
Hi all!

I have a feature on my site that uses a very simple javascript script. The data used by the script changes every month. I'm trying to figure out how I can combine them into one js application so that I don't have to change it every month. I've figured most of this out, but it uses a line that I'm not sure what its function is. It uses a percent sign and I can't find any reference to what this does. Google won't let you search for special characters, and the only references to the word "percent" don't make sense in this context. I'm hoping someone here can shed some light on what it does.

Here's an example of the code. The script basically displays a different bit of text depending for each day of the month. It defines an array (ar), lists the data, gets today's date, determines the day number and writes the corresponding text from the array on the webpage. But before it writes the text, it seems to test for the length of the array??? Can someone tell me what that line (num %=ar.length) does and what the percentage sign does?

var ar = new Array("March 1st", "March 2nd", "March 3rd", ... "March 31st");
var now = new Date();
var num = now.getDate() - 1;

num %= ar.length;

document.write(ar[num]);


Edited: I just noticed that %= seems to be an assignment operator that assigns the modulus (remainder), but I'm still confused about what that would do in this case or why it's needed. Still hoping someone can explain this to me.

Thanks in advance! :)

Starhugger

< Message edited by Starhugger -- 2/28/2009 17:01:19 >
BeTheBall

 

Posts: 6502
Joined: 6/21/2002
From: West Point Utah USA
Status: offline

 
RE: Javascript help - 2/28/2009 22:35:38   
Pretty good explanation here:

http://www.w3schools.com/jS/js_operators.asp

This:

num %= ar.length;

is the same as:

num = num % ar.length;

To be honest it seems backwards to me as num will always be less than ar.length. Generally, I would expect the larger number to be first when trying to get the remainder. Wonder if it shouldn't be:

num = ar.length % num;



_____________________________

Duane

Some people are like Slinkies . . . Not really good for anything . . . . . But they still bring a smile to your face when you push them down a flight of stairs.

(in reply to Starhugger)
Starhugger

 

Posts: 661
Joined: 4/12/2005
From: Canada
Status: offline

 
RE: Javascript help - 2/28/2009 23:49:35   
Hi Duane,

Thanks for your reply. The w3schools page was where I learned that it was an assignment operator, but it still doesn't make sense to me in this context. It's not the modulus that you'd want, it's simply the array record corresponding to the day number (minus one, since the records are numbered starting with zero).

For the record, I didn't write the original script. I could ask the person who licenses the data but I think she got someone else to write the script so she probably don't know what it does either.

Strangely, the script works. It does indeed return the correct array record, and when I take out that line it returns "undefined."

If the day number (num) is 3 and there are 31 records, then wouldn't num %= ar.length be 1? 1 is what's left over after 31 is divided by 3. So I don't understand why/how this works. What I would want is the 3rd record, but this would return the 2nd record (zero being the first record). It just doesn't make sense to me.

Starhugger




(in reply to BeTheBall)
BeTheBall

 

Posts: 6502
Joined: 6/21/2002
From: West Point Utah USA
Status: offline

 
RE: Javascript help - 3/1/2009 0:11:09   
So what happens if you use just this instead?

var ar = new Array("March 1st", "March 2nd", "March 3rd", ... "March 31st");
var now = new Date();
var num = now.getDate() - 1;

document.write(ar[num]);

_____________________________

Duane

Some people are like Slinkies . . . Not really good for anything . . . . . But they still bring a smile to your face when you push them down a flight of stairs.

(in reply to Starhugger)
Starhugger

 

Posts: 661
Joined: 4/12/2005
From: Canada
Status: offline

 
RE: Javascript help - 3/1/2009 12:08:45   
Hi Duane,

That's what I meant when I said it returns "undefined." When I remove the "mystery line" and leave the rest in, it displays "undefined" on the screen. Weird, eh?

SH


(in reply to BeTheBall)
TexasWebDevelopers

 

Posts: 761
Joined: 2/22/2002
From: Dallas, TX
Status: offline

 
RE: Javascript help - 3/1/2009 14:43:25   
Assuming today is March 1:
If you wrote out the variables they would look like this:
ar = March 1st,March 2nd,March 3rd
now = Sun Mar 1 13:27:10 CST 2009
num = 0 (Returns the day as a numeral so "var num = now.getDate() - 1;" is really "1-1=0")
num %= ar.length checks to see if it is an array and not a string
ar[num] = 0 and since 0 is the first element of the array it writes "March 1st"

_____________________________

:)

Follow us on TWITTER

(in reply to Starhugger)
Starhugger

 

Posts: 661
Joined: 4/12/2005
From: Canada
Status: offline

 
RE: Javascript help - 3/1/2009 14:56:54   
Ah, thank you! That makes a bit more sense. However, it leads to another question: why does it need to test if ar is an array when it was already defined as one?

SH


(in reply to TexasWebDevelopers)
TexasWebDevelopers

 

Posts: 761
Joined: 2/22/2002
From: Dallas, TX
Status: offline

 
RE: Javascript help - 3/1/2009 15:38:28   
ar.length is checking to see if num is an array not a string.
Then this bit "ar[num]" calls the specific element in your your ar array.

_____________________________

:)

Follow us on TWITTER

(in reply to Starhugger)
Starhugger

 

Posts: 661
Joined: 4/12/2005
From: Canada
Status: offline

 
RE: Javascript help - 3/1/2009 16:28:36   
(scratching head) Okay, sorry to ask you to keep explaining this but somehow I'm not getting it. I do understand that "ar[num]" retrieves the num-th element of the ar array. But you're saying "ar.length" wants to know the data type of "num"? Num is actually an integer (the day of the month), so it is neither an array nor a string.

I'm also still confused why w3schools say %= is an assignment operator, which would take the value of "ar.length" and put it in "num." But that seems very different from what you're saying.

I do have a programming background (albeit ancient in computer-years :) ) so I'm familiar with programming logic and some of the syntax, but somehow this isn't making sense to me.

SH


(in reply to TexasWebDevelopers)
TexasWebDevelopers

 

Posts: 761
Joined: 2/22/2002
From: Dallas, TX
Status: offline

 
RE: Javascript help - 3/1/2009 21:39:50   
Sorry I wasn't as clear as I could have been.
You are correct - you are assigning a value to num (that's the %= modulus bit).
ar.length makes sure the assignment may be used in this array.
For example, if you substitute your existing definition of the var num from this:
var num = now.getDate() - 1;
to this:
var num =2;
You script would still run.
However, this:
var num='k' would return
"NaNundefined"

< Message edited by TexasWebDevelopers -- 3/1/2009 21:48:52 >


_____________________________

:)

Follow us on TWITTER

(in reply to Starhugger)
BeTheBall

 

Posts: 6502
Joined: 6/21/2002
From: West Point Utah USA
Status: offline

 
RE: Javascript help - 3/2/2009 9:10:32   
Again, if the purpose is solely to write the value in the array that corresponds to the current date, then this seems to work for me.

<html>
<head>
<script type="text/javascript">
var ar = new Array("March 1st", "March 2nd", "March 3rd", "March 4th", "March 5th", "March 6th", "March 7th", "March 8th", "March 9th", "March 10th", "March 11th", "March 12th", "March 13th", "March 14th", "March 15th", "March 16th", "March 17th", "March 18th", "March 19th", "March 20th", "March 21st", "March 22nd", "March 23rd", "March 24th", "March 25th", "March 26th", "March 27th", "March 28th", "March 29th", "March 30th", "March 31st");
var now = new Date();
var num = now.getDate() - 1;

</script>
</head>
<body>
<script type="text/javascript">
document.write(ar[num]);
</script>
</body>
</html>


_____________________________

Duane

Some people are like Slinkies . . . Not really good for anything . . . . . But they still bring a smile to your face when you push them down a flight of stairs.

(in reply to TexasWebDevelopers)
TexasWebDevelopers

 

Posts: 761
Joined: 2/22/2002
From: Dallas, TX
Status: offline

 
RE: Javascript help - 3/2/2009 9:26:47   
In your current code that would work. Perhaps the original code contained input from fields or a mixed array. Anyway, the length property does not include string-indexed array numbers so you can't iterate through the array using a numerical for loop. Instead you would use the in enumerator to iterate through the array as if it were an object--of course, an array is an object and the members of the array are simply subsets of that object's properties. The length property is the total of that subset which is why string-indexed members aren't included.

_____________________________

:)

Follow us on TWITTER

(in reply to BeTheBall)
Starhugger

 

Posts: 661
Joined: 4/12/2005
From: Canada
Status: offline

 
RE: Javascript help - 3/2/2009 11:17:06   
Thanks Duane! I tried it out on actual data I have and it seems to work. I wonder why I couldn't get it to work before? My computer must have hiccupped. I'm actually defining several arrays, one for each month, and I have an If...then...else selecting which array to take the data from, depending on the month. This means I won't have to change it every month, and I won't have to worry about the dateline differences either. This seems to be working, using your code as a basic structure. Thanks so much! :)

Starhugger


(in reply to BeTheBall)
Starhugger

 

Posts: 661
Joined: 4/12/2005
From: Canada
Status: offline

 
RE: Javascript help - 3/2/2009 11:40:16   
quote:

ORIGINAL: TexasWebDevelopers

In your current code that would work. Perhaps the original code contained input from fields or a mixed array. Anyway, the length property does not include string-indexed array numbers so you can't iterate through the array using a numerical for loop. Instead you would use the in enumerator to iterate through the array as if it were an object--of course, an array is an object and the members of the array are simply subsets of that object's properties. The length property is the total of that subset which is why string-indexed members aren't included.

TWD, the only purpose of the script is to display a text string that corresponds to the day of the month, so that a different string displays each day. There's no need for a "for" loop, which may be why the original programmer didn't index the array elements (it wasn't needed).

Do you mean that the "length" will essentially count the number of elements in the array? So in the case of a March array (one for each day), the "length" will be 31, correct? If so, it's still a puzzle to me why the original programmer would want to test how many elements are in the array (unless it's to troubleshoot the possibility of an empty array or an array with too few elements for the day number, but there's nothing in the code to handle the error if that were the case). It still puzzles me too why "num" would become the *modulus* of dividing the number of elements ("ar.length") by the day number (initial value of "num"). What puzzles me even more is why/how the script still works properly that way.

*shrug* I guess I should make a point of learning Javascript properly one of these days. It wouldn't be a long learning curve for me, it just would take time I never seem to have. It's becoming yet another entry on a long list of things I'm going to have to put off to my next lifetime. :)

Thanks for the help!

Starhugger


(in reply to TexasWebDevelopers)
TexasWebDevelopers

 

Posts: 761
Joined: 2/22/2002
From: Dallas, TX
Status: offline

 
RE: Javascript help - 3/2/2009 14:29:44   
lots of kiddie scripters out there--glad to meet someone like you who cares.

_____________________________

:)

Follow us on TWITTER

(in reply to Starhugger)
Page:   [1]

All Forums >> Web Development >> General Web Development >> Javascript help
Page: [1]
Jump to: 1





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts