|
| |
|
|
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.
|
|
|
|
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
|
|
|
|
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.
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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.
|
|
|
|
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
|
|
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
|
|
|