/    Sign up×
Community /Pin to ProfileBookmark

Question: Calculation Problem

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

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@vickers_bitsFeb 05.2003 — use % to get the remainder

eg:

2003 % 3 would return 2

hope this helps
Copy linkTweet thisAlerts:
@andrewkooiauthorFeb 05.2003 — 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
Copy linkTweet thisAlerts:
@CharlesFeb 05.2003 — [font=georgia]I count three different errors there, and the [font=monospace]if[/font]statement is not the way to go. Try:

[font=monospace]

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

[/font]

And keep in mind that this, like all JavaScript, will fail one or so in ten times.[/font]
Copy linkTweet thisAlerts:
@khalidali63Feb 05.2003 — In addition to the above one should not write that much inline javascript as well.

cheers

Khalid
Copy linkTweet thisAlerts:
@CharlesFeb 05.2003 — [i]Originally posted by khalidali63 [/i]

[B]In addition to the above one should not write that much inline javascript as well.



cheers



Khalid [/B]
[/QUOTE]
[font=georgia]Why not? It does no harm.[/font]
Copy linkTweet thisAlerts:
@khalidali63Feb 05.2003 — 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
Copy linkTweet thisAlerts:
@andrewkooiauthorFeb 05.2003 — Interesting observation. Just wondering how you would make this code into a function when it is specific towards a particular webpage.
×

Success!

Help @andrewkooi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...