#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0

    JS Only first 3 functions are called within another function


    The file I am working on runs strictly on the client side to automate recalculating of dates for a simulation. When I click a button it calls a function:

    Code:
    <button onclick='CalcDates();'>Calculate Dates</button>
    The CalcDates function has 6 functions. Only the first 3 run. They all work as I have rearranged them and the first 3 always run, although GetDaysToActivity(); always has to be first as the others are based on this calculation.

    Code:
    <script type="text/javascript">
        function CalcDates()
        {
            GetDaysToActivity();
            ActivityDate0();
            ActivityDate1();
            ActivityDate148();
            ActivityDate156();
            ActivityDate200();
        }
    </script>
    I a cannot find any reference on a limit to functions in a function. What would I have to do to make them all work. Thank you.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    there is no such limit in JavaScript. The problem is caused by the functions themselves.

    You said the functions depend on each other, but I don't see you passing around arguments and return values. So you're obviously working with implicit dependencies somewhere behind the scenes. This is a very bad idea, especially in JavaScript.

    But we can't give you concrete advice without seeing the actual code.
    Last edited by Jacques1; January 1st, 2014 at 09:43 PM.
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0
    The end-user enters the future date of the simulation into a textbox. The GetDaysToActivity calculates how many days into the future is the simulation, so when the other functions are called it correctly calculates past dates in the original time choreographed design.

    The 5 ActivityDate() functions depend on the value calculated from the GetActivityDate, but do not depend on each other.

    Here is the GetDaysToActivity function and the first ActivityDate function. The remainder are identical except for the value of x.

    Ignore the Birthdate variable names. This was an example I found of what I needed to do and I have not renamed the variables.

    Code:
    <script type="text/javascript">
        function GetDaysToActivity() //calculates number of days until the Activity date
        {
            var dob1 = document.getElementById("activitydate");
            var age1 = document.getElementById("daystoactivity");
            var ms = 86400000
            var dateAry = dob1.value.split("/");
            var birthMonth = parseInt(dateAry[0]);
            var birthDay = parseInt(dateAry[1]);
            var birthYear = parseInt(dateAry[2]);
            var birthDate = new Date(birthYear, birthMonth - 1, birthDay);
            var currentDate = new Date();
            var age = birthDate - currentDate;  // result is number of milliseconds difference
            var mtg = age/ms;
                mtg = Math.ceil(mtg);
    
                if(birthDate < (currentDate + 1)) {
                    alert("Activity date cannot be in the past");
                    document.getElementById("activitydate").value ="";
                    document.printchart.activitydate.focus();
                    }
                    else {
                        age1.value = mtg;
                    }
        }
    </script>
    
    <script type="text/javascript">
        function ActivityDate0() //calculates the dates in the text
        {
            var date = new Date();     
            var ms = 86400000 
            var mtg = document.getElementById("daystoactivity").value;
            if (mtg < 1){
                mtg = 0
                };
            var x = 0;
            var daysback = x - mtg;
            var nd = date - (daysback * ms);
            nd = new Date(nd);
            var curr_day = ("0" + nd.getDate()).slice(-2);
            var curr_month = ("0" + (nd.getMonth() + 1)).slice(-2);
            var curr_year = nd.getFullYear();
            //return curr_month + "/" + curr_day + "/" + curr_year;
            var newdate = curr_month + "/" + curr_day + "/" + curr_year;
            document.getElementById('0a').innerHTML = newdate   //Lab 1
    		document.getElementById('0b').innerHTML = newdate   //Lab 2
    		document.getElementById('0c').innerHTML = newdate   //Hepatology consult
    
        }
    </script>
    
    <body>
    
    <div>
    <label>Future date of activity (mm/dd/yyyy): &nbsp;</label>
        <input type="text" id="activitydate" name="activitydate" />
        
        <input type="hidden" id="daystoactivity" name="daystoactivity" style="display: none;" />
        <button onclick='CalcDates();'>Calculate Dates</button>
    In about 30 places throughout 27 pages is the id's for each of the date like this:

    Code:
    <td id="0a">Today</td>
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    And what makes you think some of the functions “do not run”? If you literally mean that they're not executed, this is impossible unless the third function crashes. Open the JavaScript console of your browser to check that. Or do you merely mean that you're not seeing the calculated results? That's a completely different thing. You're probably not writing the results to the right elements.

    I think the main problem is that you lack basic programming knowledge:

    • If a function is dependent on a certain value, you use a parameter. You do not duplicate the function definition for every possible value. As a rule of thumb: When you find yourself numbering function names, you're doing it wrong.
    • If you want to pass a value from one function to another, you have the first function return the value and then pass it as an argument to the second function. You do not write the value to some hidden field (how would you do that in other programming languages?).
    • type="hidden" actually means that the field is not visible. What's the point of display: none then? Is that supposed to make it “even more invisble”?
    • The date calculations are very weird. Why don't you simply subtract the number of days from the target date? Instead, you calculate the number of days between the current date and the target date, then subtract the specified number of days, and then add this back to the current date, using microseconds each time. What?
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0
    Jacques1, thank you for the response.

    I think the main problem is that you lack basic programming knowledge:
    I cannot disagree with you, I have no knowledge in javascript. My doctorate is in the medical field. Everything you see was examples found on the Internet (good or bad). I am only trying to help a collegue who has a short deadline and the University programmers cannot get to it soon enough.

    Yes, I typically write one function and pass the value to the function. In these simulations the end user never knows when she will get the chance to print out the simulation and "today" always has to be the date of the simulation, but if the file is printed 3 days in advance and and she is printing it today(1/2/2014), then the 0 days should print as 1/5/2014, and the choreographed dates need to be calculated from that date backwards.

    I could not figure out how to call each date from one function within the body of the text without it calculating a date when the page is loaded without knowing the target date. So if I wanted the simulation date to be yesterday (1/4/2014). How would I put this in the body of text?
    Code:
    Date: <script type="text/javascript">
    document.write(ActivityDate(1, GetActivityDate))
    </script>
    Thank you for the comments and direction. I will keep plugging away with my lack of programming knowledge.
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Not sure if I understand you correctly. You have a certain reference date which is the "day 0". And in your script, you add a certain numbers of days to that date. Where does the current date come into play? What do you need that for?

    The script above could simply be written like this:

    javascript Code:
    function renderDate(referenceDate, days) {
        // duplicate the reference date, then add the number of days;
        // JavaScript will automatically take care of switching to the next month or year if the current one is exceeded
        var endDate,
            formattedDate;
     
        endDate = new Date(referenceDate.getTime());
        endDate.setDate(endDate.getDate() + days);
     
        formattedDate = endDate.getDate() + '/' + (endDate.getMonth() + 1) + '/' + endDate.getFullYear();
     
        alert('Calculated Date: ' + formattedDate);
    }
     
     
     
    var testString = '1/5/2014';
     
    var dateParts = testString.split('/');
    var referenceDate = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);
     
    // show the date coming 5, 10, 15 days after the reference date
    renderDate(referenceDate, 5);
    renderDate(referenceDate, 10);
    renderDate(referenceDate, 15);
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0
    Jacques,
    Thank you for trying to help. It is good for the instructor to be the student to keep oneself humble.

    I am sorry for the confusion about this simulation. It is a medical simulation that resembles a medical chart about a patient admitted to the hospital last last night but the student is "seeing them today" (Day0/reference date) This gets printed out before the simulation so the instructor has time to compile the paper chart. There are 27 pages of printed material, each which has one or more dates that refer back in time, although there are only 5 or 6 different dates. These dates would appear on labs, progress notes, medication records, etc.

    Example: If the day of the simulation is going to be January 10th and the instructor has time to print and compile on January 6.

    Day 0 Patient in hospital Reference date(Day0) - 0(days back in time) + 4 (days between print and simulation date) = January 10, 2014
    Day(1) Patient was seen in ER and admitted yesterday Day(0) - 1 +4 = 1/9/2014
    Day(7) Patient was seen in clinic = 1/3/2014
    Day(148) Patient was seen by some doctor = 8/15/2013
    Day(200) Patient seen in clinic = 6/24/2013

    The end-user enters the future reference date in a textbox and then clicks a button to calculate all the dates at once(at least how I would like it to work).

    In your example the function fires on page load, but the reference date is not yet known, nor how many days in the future is the reference date from the date the simulation is printed.
    I also don't understand how you can pass parameters to a function if they are unknown when the page loads. This is the basic problem which lead me to this poor solution.

    I made a unknowledgeable attempt are making your example into how I think it should be.
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <script>
        function renderDate(referenceDate, days) {
            // duplicate the reference date, then add the number of days;
            // JavaScript will automatically take care of switching to the next month or year if the current one is exceeded
            var endDate,
                formattedDate;
         
            endDate = new Date(referenceDate.getTime());
            endDate.setDate(endDate.getDate() + days);
         
            formattedDate = endDate.getDate() + '/' + (endDate.getMonth() + 1) + '/' + endDate.getFullYear();
         
            //alert('Calculated Date: ' + formattedDate);
        }
         
        function getDate() {
        var testString = document.getElementById("activitydate").value; 
        //var testString = '1/5/2014';
         
        var dateParts = testString.split('/');
        var referenceDate = new Date(dateParts[1] - 1, dateParts[2], dateParts[0]);  //modified to format mm/dd/yyyy
        } 
    </script>
    </head>
    <body>
    
    <label>Future date of activity (mm/dd/yyyy): &nbsp;</label>
        <input type="text" id="activitydate" name="activitydate" /> (Reference Date)
        
       <p> <button onclick='GetDate();'>Calculate Dates</button><br /></p>
    
        <!--// show the date coming 5, 10, 15 days after the reference date -->
        Date of patient seen today : <script>renderDate(referenceDate, 0);</script> <br /> 
        Date when patient was seen yesterday: <script>renderDate(referenceDate, -1);</script> <br />
        Date when patietn was seen 15 days ago: <script>renderDate(referenceDate, -15); </script>
    </body>
    </html>
    Thank you again for trying to help.http://forums.devshed.com/newreply.php#
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0
    Although this is still my very long poor design, I did get this simplified version to work. Maybe this will help demonstrate what I am trying to accomplish.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
    	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    	<meta name="author" content="Robert Shaw" />
    
    	<title>Print Chart Test</title>
       
    <script type="text/javascript">
        function printDiv() {
            var printContents = document.getElementById("PrintArea").innerHTML;    
            var originalContents = document.body.innerHTML;      
            document.body.innerHTML = printContents;     
            window.print();     
            document.body.innerHTML = originalContents;
        }
    </script>
    
    <script type="text/javascript">
        function GetDaysToActivity() //calculates number of days until the Activity date
        {
            var dob1 = document.getElementById("activitydate");
            var age1 = document.getElementById("daystoactivity");
            var ms = 86400000
            var dateAry = dob1.value.split("/");
            var birthMonth = parseInt(dateAry[0]);
            var birthDay = parseInt(dateAry[1]);
            var birthYear = parseInt(dateAry[2]);
            var birthDate = new Date(birthYear, birthMonth - 1, birthDay);
            var currentDate = new Date();
            var age = birthDate - currentDate;  // result is number of milliseconds difference
            var mtg = age/ms;
                mtg = Math.ceil(mtg);
    
                if(birthDate < (currentDate + 1)) {
                    alert("Activity date cannot be in the past");
                    document.getElementById("activitydate").value ="";
                    document.printchart.activitydate.focus();
                    }
                    else {
                        age1.value = mtg;
                    }
        }
    </script>
    
    <script type="text/javascript">
        function ActivityDate0() //calculates the dates in the text
        {
            var date = new Date();     
            var ms = 86400000 
            var mtg = document.getElementById("daystoactivity").value;
            if (mtg < 1){
                mtg = 0
                };
            var x = 0;
            var daysback = x - mtg;
            var nd = date - (daysback * ms);
            nd = new Date(nd);
            var curr_day = ("0" + nd.getDate()).slice(-2);
            var curr_month = ("0" + (nd.getMonth() + 1)).slice(-2);
            var curr_year = nd.getFullYear();
            //return curr_month + "/" + curr_day + "/" + curr_year;
            var newdate = curr_month + "/" + curr_day + "/" + curr_year;
            document.getElementById('0a').innerHTML = newdate   //Lab 1
    
        }
    </script>
    <script type="text/javascript">
        function ActivityDate1() //calculates the dates in the text
        {
            var date = new Date();     
            var ms = 86400000 
            var mtg = document.getElementById("daystoactivity").value;
            if (mtg < 1){
                mtg = 0
                };
            var x = 1;   // Yesterday's date
            var daysback = x - mtg;
            var nd = date - (daysback * ms);
            nd = new Date(nd);
            var curr_day = ("0" + nd.getDate()).slice(-2);
            var curr_month = ("0" + (nd.getMonth() + 1)).slice(-2);
            var curr_year = nd.getFullYear();
            var newdate = curr_month + "/" + curr_day + "/" + curr_year;
            document.getElementById('1a').innerHTML = newdate   //Lab3
        }
    </script>
    
    <script type="text/javascript">
        function ActivityDate148() //calculates the dates in the text
        {
            var date = new Date();     
            var ms = 86400000 
            var mtg = document.getElementById("daystoactivity").value;
            if (mtg < 1){
                mtg = 0
                };
            var x = 148;
            var daysback = x - mtg;
            var nd = date - (daysback * ms);
            nd = new Date(nd);
            var curr_day = ("0" + nd.getDate()).slice(-2);
            var curr_month = ("0" + (nd.getMonth() + 1)).slice(-2);
            var curr_year = nd.getFullYear();
            var newdate = curr_month + "/" + curr_day + "/" + curr_year;
            document.getElementById('148a').innerHTML = newdate
        }
    </script>
    
    <script type="text/javascript">
        function ActivityDate156() //calculates the dates in the text
       {
            var date = new Date();     
            var ms = 86400000 
            var mtg = document.getElementById("daystoactivity").value;
           if (mtg < 1){
                mtg = 0
                };
            var x = 156;
            var daysback = x - mtg;
            var nd = date - (daysback * ms);
            nd = new Date(nd);
            var curr_day = ("0" + nd.getDate()).slice(-2);
            var curr_month = ("0" + (nd.getMonth() + 1)).slice(-2);
            var curr_year = nd.getFullYear();
           var newdate = curr_month + "/" + curr_day + "/" + curr_year;
            document.getElementById('156a').innerHTML = newdate
        }
    </script>
    
    <script type="text/javascript">
        function ActivityDate200() //calculates the dates in the text
        {
            var date = new Date();     
            var ms = 86400000 
            var mtg = document.getElementById("daystoactivity").value;
            if (mtg < 1){
                mtg = 0
                };
            var x = 200;
            var daysback = x - mtg;
            var nd = date - (daysback * ms);
            nd = new Date(nd);
            var curr_day = ("0" + nd.getDate()).slice(-2);
            var curr_month = ("0" + (nd.getMonth() + 1)).slice(-2);
            var curr_year = nd.getFullYear();
            var newdate = curr_month + "/" + curr_day + "/" + curr_year;
            document.getElementById('200a').innerHTML = newdate
        }
    </script>
    
    <script type="text/javascript">
        function CalcDates()
        {
            GetDaysToActivity();
            ActivityDate0();
            ActivityDate1();
            ActivityDate148();
            ActivityDate156();
            ActivityDate200();
        }
    </script>
    
    </head>
    <body>
    
    <div>
    <label>Future date of activity (mm/dd/yyyy): &nbsp;</label>
        <input type="text" id="activitydate" name="activitydate" />
        
        <input type="hidden" id="daystoactivity" name="daystoactivity" />
        
        <button onclick='CalcDates();'>Calculate Dates</button>
        
    	</div>
        <br />
    	Chart starts below
    	<hr />
    	<!-- *****START OF PRINTED CHART ***** -->
        <h2 align="center"> MEDICAL CHART</h2>
    
    		<table width="700px" border="0" cellpadding="1" cellspacing="0">
                        <tr>
                            <td width="10%">Provider:</td>
                            <td width="15%">Smith, MD</td>
                            <td width="35%">Date Seen today:</td>
                            <td id="0a" width="30%">Today</td>
                        </tr>
    		</table>
            		<table width="700px" border="0" cellpadding="1" cellspacing="0">
                        <tr>
                            <td width="10%">Provider:</td>
                            <td width="15%">Smith, MD</td>
                            <td width="35%">Date seen yesterday:</td>
                            <td id="1a" width="30%">Yesterday</td>
                        </tr>
    		</table>
            		<table width="700px" border="0" cellpadding="1" cellspacing="0">
                        <tr>
                            <td width="10%">Provider:</td>
                            <td width="15%">Smith, MD</td>
                            <td width="35%">Date seen 148 days ago:</td>
                            <td id="148a" width="30%">148 days ago</td>
                        </tr>
    		</table>
            		<table width="700px" border="0" cellpadding="1" cellspacing="0">
                        <tr>
                            <td width="10%">Provider:</td>
                            <td width="15%">Smith, MD</td>
                            <td width="35%">Date seen 156 days ago:</td>
                            <td id="156a" width="30%">156 days ago</td>
                        </tr>
    		</table>
            		<table width="700px" border="0" cellpadding="1" cellspacing="0">
                        <tr>
                            <td width="10%">Provider:</td>
                            <td width="15%">Smith, MD</td>
                            <td width="35%">Date seen 200 days ago:</td>
                            <td id="200a" width="30%">200 days ago</td>
                        </tr>
    		</table>
    	<!-- ***** END OF PRINTED CHART ********* -->
    </body>
    </html>
  16. #9
  17. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,696
    Rep Power
    1959
    Originally Posted by Waubain
    Day 0 Patient in hospital Reference date(Day0) - 0(days back in time) + 4 (days between print and simulation date) = January 10, 2014
    Day(1) Patient was seen in ER and admitted yesterday Day(0) - 1 +4 = 1/9/2014
    Day(7) Patient was seen in clinic = 1/3/2014
    Day(148) Patient was seen by some doctor = 8/15/2013
    Day(200) Patient seen in clinic = 6/24/2013
    In you description above, you refer to day 1, 7, 148 and 200.
    In you code you refer to day 1, 148, 156 and 200.

    Where does these number come from?

    Looks like there are a lot of information hardcoded directly into the code without any explanation.
    Maybe you can explain:
    - What the user has to type in?
    - Which parameters are known to the program directly?


    I suggest you rename your variable names so it describe the content/purpose. This will help to understand the code during reading.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0
    I have found a University programmer to help me on this project. I want to thank everyone who tried to help me move this project forward.

    Waubain
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0
    The light bulb finally came on. I see the poor logic in my design that you were trying to tell me. I do not need the current date as it has no meaning. I only need to count back from the reference date.

    Thanks again for your continued attempts to try to understand the illogical.

    Comments on this post

    • Jacques1 agrees : Thanks for the feedback.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0
    Jacques1,
    I have more experience in Visual Basic, and yes, I would have never wrote code like I did above. I started over and wrote this out in VB and then roughly translated it back to JS. It still may not be the best programming based on my very limited knowledge, but was done in only 2 functions.

    Still not quitting my day job.
    ..Cheers.

    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <script type="text/javascript">
    
        function ChangeDates() 
        {
        //gets string from activitydate Textbox and formats into date as variable
        var strDate = document.getElementById("activitydate").value;  //entered at Month/Day/Year
        var referenceDate = new Date(Date.parse(strDate)); // returns date from string
            
        //to be placed in the proper location of body
        
        var a = [0,1,129,147,148,156,200];
        
        for (var i=0, tot=a.length; i < tot; i++)
          {
             switch (a[i])
            {   
                case 0: 
                    var Date0 = renderNewDate(referenceDate, 0) 
                    document.getElementById('day0').innerHTML = Date0;
                break;
    
                case 1: 
                    var Date1 = renderNewDate(referenceDate, 1) 
                    document.getElementById('day1').innerHTML = Date1;
                break;
                
                case 129: 
                    var Date1 = renderNewDate(referenceDate, 129) 
                    document.getElementById('day129').innerHTML = Date1;
                break;
            
                case 147: 
                    var Date147 = renderNewDate(referenceDate, 147) 
                    document.getElementById('day147').innerHTML = Date147;
                break;
            
                case 148: 
                    var Date148 = renderNewDate(referenceDate, 148) 
                    document.getElementById('day148').innerHTML = Date148;
                break;
            
                case 156: 
                    var Date156 = renderNewDate(referenceDate, 156) 
                    document.getElementById('day156').innerHTML = Date156;
                break;
            
                case 200: 
                    var Date200 = renderNewDate(referenceDate, 200) 
                    document.getElementById('day200').innerHTML = Date200;
                break;
            }
          }
        }
    </script>
     <script type="text/javascript">         
        function renderNewDate(referenceDate, days) {
            // duplicate the reference date, then subtract the number of days;
            // JavaScript will automatically take care of switching to the next month or year if the current one is exceeded
            
            var endDate,
                formattedDate,
                calculatedDate;
            
            endDate = new Date(referenceDate.getTime());
            endDate.setDate(endDate.getDate() - days);
                 
            calculatedDate = (endDate.getMonth() + 1) + '/' + endDate.getDate() + '/' + endDate.getFullYear();
            return calculatedDate;
            }
    </script>
    </head>
    
    <body>
    <label>Future date of activity (mm/dd/yyyy): &nbsp;</label>
        <input type="text" id="activitydate" name="activitydate" />
        <br />
        <button onclick="ChangeDates()">Change Dates</button>
        <br />
       <br />
       
       <table>
        <tr>
            <td>Today's date:&nbsp</td>
            <td id="day0"> new date</td>
        </tr>
            <tr>
            <td>Yesterday's date:&nbsp</td>
            <td id="day1"> new date</td>
        </tr>
            <tr>
            <td>129 days ago date:&nbsp</td>
            <td id="day129"> new date</td>
        </tr>
            <tr>
            <td>147 days ago date:&nbsp</td>
            <td id="day147"> new date</td>
        </tr>
            <tr>
            <td>148 days ago date:&nbsp</td>
            <td id="day148"> new date</td>
        </tr>
            <tr>
            <td>156 days ago date:&nbsp</td>
            <td id="day156">new date</td>
        </tr>
            <tr>
            <td>200 days ago date:&nbsp</td>
            <td id="day200"> new date</td>
        </tr>
       </table>
    </body>
    </html>
  24. #13
  25. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    The loop/switch combination makes no sense.

    It looks like you couldn't decide between using a loop and writing down the steps one after another. Now it's a strange mixture of both.

    The whole point of a loop is to not write down all steps. Instead, you write down the general procedure and then repeat it in a certain way. For example, counting from 1 to 10 in pseudo code would be

    Code:
    for i from 1 to 10:
    	print i
    Alternatively, you could of course write down the print command 10 times:

    Code:
    print 1
    print 2
    print 3
    ...
    print 10
    What you do, however, is first write down a loop. And then you write down the individual steps and make the loop execute them one after another:

    Code:
    for i from 1 to 10:
    	if i = 1:
    		print 1
    	else if i = 2:
    		print 2
    	else if i = 3:
    		print 3
    	...
    	else if i = 10:
    		print 10
    This obviously makes no sense. Either you use a loop, or you write down the steps one after another. But not both.

    In your case, the loop would be

    javascript Code:
    var differences = [0,1,129,147,148,156,200];
     
    for (var i = 0; i < differences.length; i++) {
    	var difference = differences[i];
     
    	var renderedDate = renderNewDate(referenceDate, difference);
    	document.getElementById('day' + difference).innerHTML = renderedDate;
    }
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo