Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 2 of 2

03232013, 08:32 PM #1
 Join Date
 Mar 2013
 Posts
 55
 Thanks
 27
 Thanked 0 Times in 0 Posts
Is it my IF statements that are causing my varibles to multiply?
I am making a game and I want the day to change day by day after each time the player presses a button. For example, "03/21/1741" then "03/22/1741" then "03/23/1741", and so on. It is important to make single digit numbers in this date occur with a zero before the digit(for example, a "7" would be "07" and "3" would be "03"). My javascript function skips around, and then starts multiplying the days by five rather than going one by one. I have been looking at it for hours and can't seem to find what I did wrong, but I assume that I am somehow getting stuck within the loop and that's what is causing the multiplying. This is my function for the date...
Code:function MorForm() { if(d=="31"){ d="01"; if(m=="10"){m="11"} if(m=="08"){m="09"} if(m=="07"){m="08"} if(m=="05"){m="06"} if(m=="03"){m="04"} if(m=="01"){m="02"} if(m=="12"){ m="01"; y=y+1; } subForm(); }; if(d=="30"){ if(m=="11"){ m="12"; d="01"; subForm(); } if(m=="09"){ m="10"; d="01"; subForm(); } if(m=="06"){ m="07"; d="01"; subForm(); } if(m=="04"){ m="05"; d="01"; subForm(); } d="31"; subForm(); } if(d=="29"){ d="30"; subForm(); } if(d=="28"){ if(m=="02"){ d="01"; m="03"; subForm(); } d="29"; subForm(); } if(d=="27"){d="28"} if(d=="26"){d="27"} if(d=="25"){d="26"} if(d=="24"){d="25"} if(d=="23"){d="24"} if(d=="22"){d="23"} if(d=="21"){d="22"} if(d=="20"){d="21"} if(d=="19"){d="20"} if(d=="18"){d="19"} if(d=="17"){d="18"} if(d=="16"){d="17"} if(d=="15"){d="16"} if(d=="14"){d="15"} if(d=="13"){d="14"} if(d=="12"){d="13"} if(d=="11"){d="12"} if(d=="10"){d="11"} if(d=="09"){d="10"} if(d=="08"){d="09"} if(d=="07"){d="08"} if(d=="06"){d="07"} if(d=="05"){d="06"} if(d=="04"){d="05"} if(d=="03"){d="04"} if(d=="02"){d="03"} if(d=="01"){d="02"} subForm(); }
03232013, 10:11 PM
#2
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 18,080
 Thanks
 203
 Thanked 2,542 Times in 2,520 Posts
It should be obvious that your code is extremely longwinded, repetitive and inefficient. And wrong.
If you want to manipulate dates, you should use the Javascript date object. In your case there is a difficulty as the year 1741 is before the year 1970 which is the start of "epoch time". However, you can overcome this by making the year 2041 for the purposes of the date object, and then subtracting 300 (years) for the final display. If you wished to display the name of the day, you would need to identify a year which began on the same day as 1/1/1741, and use that instead, deducting the appropraite number of years.
My understanding is that you wish the displayed date of 03/21/1741 to advance by one day at each click of the button.
I expect you realise that if the user refreshes/reloads the page, then the date reverts to its starting value. You could use a cookie to carry the last date forward.Code:<span id = "myspan"></span> <br><br> <input type = "button" value = "Advance the date by one day" onclick = "advance()"> <script type = "text/javascript"> var d = new Date (2041,02,21); // months in Javascript are 011 pretty(); function pretty() { var yr = d.getFullYear(); yr = yr  300; var mth = d.getMonth() +1; // months in Javascript are 011 if (mth <10) {mth = "0" + mth} var dt = d.getDate(); if (dt <10) {dt = "0" + dt} document.getElementById("myspan").innerHTML = "The date today is " + mth + "/" + dt + "/" + yr; } function advance() { d = new Date(d.getTime() + (1*24*60*60*1000)); // add one day pretty(); } </script>
"I was gratified to be able to answer promptly. I said I didn't know."  Mark Twain, US humorist, novelist, short story author, & wit (1835  1910)
Last edited by Philip M; 03232013 at 10:26 PM. Reason: Noticed typo
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.