Click to See Complete Forum and Search --> : Question: Calculation Problem


andrewkooi
02-05-2003, 01:52 AM
Greetings,

At present, I have some javascript code and HTML which looks like this (Created mostly using Dreamweaver 3.0):

function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;}

function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}}

<td align="right"><b><font face="Arial" size="3"><a href="current.html" onmouseout="MM_swapImgRestore()"
onmouseover="MM_swapImage('CurrentImage','','images/NewImage.jpg',1);;return document.MM_returnValue">Current
Image</a></font></b></td>

What happens in this code is, when the mouse touches the link Current Image, the picture will change from CurrentImage to
NewImage. What I am trying to do, and have been unable to figured out how to, is this:

Supposing this year is 2001. When I divide 2001 with the number 3, you get nothing remainder because 2001/3 = 667 and 0
remainder.
Supposing this year is 2002. When I divide 2002 with the number 3, you get 1 remainder because 2002/3 = 667 and 1
remainder.
Supposing this year is 2003. When I divide 2003 with the number 3, you get 2 remainder because 2002/3 = 667 and 2
remainder.
Depending on what is the remaining number, I would like a different image to be swapped.


Hoping someone out there can help resolve this. Thanx in advance for your help.

Best Regards,

Andrew

vickers_bits
02-05-2003, 02:01 AM
use % to get the remainder

eg:
2003 % 3 would return 2

hope this helps

andrewkooi
02-05-2003, 03:23 AM
Greetings,

Thanx for previous reply...

I tried using the following code:

onmouseover= If Date.getFullYear() % 3 = 1 then "MM_swapImage('CurrentImage','','images/NewImage.jpg',1);return document.MM_returnValue">Current Image</a></font></b></td>

but get error message stating If is undefined


Then I tried:

onmouseover="If Date.getFullYear() % 3 = 1 then MM_swapImage('CurrentImage','','images/NewImage.jpg',1);return document.MM_returnValue">Current Image</a></font></b></td>

but get error message stating ; expected


Just wondering how to resolve this...

Thanx once again for replies.


Regards,

Andrew

Charles
02-05-2003, 04:26 AM
I count three different errors there, and the ifstatement is not the way to go. Try:

<a href="current.html" onmouseout="MM_swapImgRestore()" onmouseover="switch (new Date().getFullYear() % 3) {case 1 : MM_swapImage('CurrentImage','','images/NewImage1.jpg',1); break; case 2 : MM_swapImage('CurrentImage','','images/NewImage2.jpg',1); break; case 3: MM_swapImage('CurrentImage','','images/NewImage3.jpg',1)}; return document.MM_returnValue">Current Image</a>

And keep in mind that this, like all JavaScript, will fail one or so in ten times.

khalidali63
02-05-2003, 04:48 AM
In addition to the above one should not write that much inline javascript as well.

cheers

Khalid

Charles
02-05-2003, 05:07 AM
Originally posted by khalidali63
In addition to the above one should not write that much inline javascript as well.

cheers

Khalid Why not? It does no harm.

khalidali63
02-05-2003, 05:10 AM
just my opinion..its not as readable and modular.MY choice would be put all the code in a function..

No other particluar reason.

cheers

Khalid

andrewkooi
02-05-2003, 08:25 AM
Interesting observation. Just wondering how you would make this code into a function when it is specific towards a particular webpage.