SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: what does the % symbol do?
-
Jan 18, 2002, 14:08 #1
- Join Date
- Dec 2000
- Location
- BOSTON MA
- Posts
- 335
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
what does the % symbol do?
does anyone know what the % percent symbol does in javascript? or how it is used?
i've searched all over the place online and looekd a coupld of books that i have but i can't seem to find an answer.. . . chris
-
Jan 18, 2002, 14:35 #2
- Join Date
- Nov 2001
- Location
- Maryland
- Posts
- 175
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
It finds the remainder in a division.
i.e. 10%2 = 0
5%2 = 1
Most often it is used to findout if a number is even or odd.
var num as whatever
if (num%2 == 0) { number is even }
else { number is odd }
Hope that helps
Drew~Drew
There Is No Greater Joy Than Soaring High On The Wings Of Your Dreams, Except Maybe The Joy Of Watching A Dreamer Who Has Nowhere To Land But In The Ocean Of Reality.
-
Jan 18, 2002, 16:19 #3
- Join Date
- Oct 2000
- Location
- Nashvegas Baby!
- Posts
- 7,845
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Fantastic answer Drew!
Kudos for your help.Adobe Certified Coldfusion MX 7 Developer
Adobe Certified Advanced Coldfusion MX Developer
My Blog (new) | My Family | My Freelance | My Recipes
-
Jan 18, 2002, 16:46 #4
- Join Date
- Feb 2000
- Location
- where the World once stood
- Posts
- 700
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi,
As Drew said, the modulos (sp?) '%' operator returns the remainder of a division.
Another use is to get a pseudo-random number:
var numOfBanners = 4;
var seed = new Date().getSeconds()
var bnr2show = seed % numOfBanners;
the above lines are from the "Reading Banner Data from a File" script/tutorial at my site. They are used instead of a random function. the bnr2show var is used to access elements within an array
VinnyWhere the World Once Stood
the blades of grass
cut me still
-
Jan 18, 2002, 22:17 #5
- Join Date
- Dec 2000
- Location
- BOSTON MA
- Posts
- 335
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thanks all very much.
the way i was seeing it get used was like this:
Code:var NumberOfImages = 10; var now = new Date() var sec = now.getSeconds() var ImageNumber = sec % NumberOfImages; ImageNumber +=1; if (ImageNumber==1) { url="portfolio.html"; Graphic="images/image.jpg"; width = "250" height = "250" } if (ImageNumber==2) etc, etc up to 10 ... document.write('<a href=\"' + url + '\">'); document.write('<img src=\"' + Graphic + '\" width='); document.write(width + ' height=' + height + ' '); document.write('border=0><br>'); document.write('</a>');
fit into the whole equation.
it looks similar to your code Vinny.
thanks again.. . . chris
-
Jan 19, 2002, 16:42 #6
- Join Date
- Feb 2000
- Location
- where the World once stood
- Posts
- 700
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi chris,
That it does. The difference is that you are adding 1 to the result and then using if/else statements (which are probably all the same except for the image information itself). You might consider placing everything into an array:
var imgInfo=new Array()
imgInfo[0]="portfolio.html|images/image.jpg|250|250";
...etc...
then you can access the data as follows:
var imgDataArray = imgInfo[imageNumber].split("|")
that would give you the following array:
imgDataArray[0] = 'portfolio.html'
imgDataArray[1] = 'images/image.jpg'
imgDataArray[2] = '250'
imgDataArray[3] = '250';
VinnyWhere the World Once Stood
the blades of grass
cut me still
-
Jan 19, 2002, 17:05 #7
- Join Date
- Dec 2000
- Location
- BOSTON MA
- Posts
- 335
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
ok, how does the value of 'image.jpg' change then?
what i mean is that there would be image1.jpg, image2.jpg all the way to image10.jpg.
wouldn't i need to add a counter to increment image.jpg?. . . chris
-
Jan 19, 2002, 20:48 #8
- Join Date
- Feb 2000
- Location
- where the World once stood
- Posts
- 700
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi,
If you have ten images, each would be in (above example) in the second cell of the main array (imageInfo): imgDataArray[1] = 'images/image.jpg'
so you would declare your array:
imgInfo[0]="...html|images/image1.jpg|...";
imgInfo[1]="...html|images/image2.jpg|...";
imgInfo[2]="...html|images/image3.jpg|...";
(note the cell reference is one less than the image number; but that's because I'm basing it upon what you said originally)
and then, using your code:
var NumberOfImages = 10;
var now = new Date()
var sec = now.getSeconds()
var ImageNumber = sec % NumberOfImages;
var imgDataArray = imgInfo[ImageNumber].split("|")
imgDataArray now holds the random image you wanted. If you wanted a specific image you would simply say:
var imgDataArray = imgInfo[aNumberFrom_0_to_9].split("|")
One of the following scripts at my site should use and explain the above methods: "Reading Banner Data..." or "Reading Data from a File."
VinnyWhere the World Once Stood
the blades of grass
cut me still
Bookmarks