SitePoint Sponsor

# Thread: what does the % symbol do?

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

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

3. Fantastic answer Drew!

Kudos for your help.

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

Vinny

5. 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>');
so i guess that would display a different image depending on the time but i couldn't figure out how the modulos fit into the whole equation.

it looks similar to your code Vinny.
thanks again.

6. 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';

Vinny

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

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

Vinny

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•