jimfog Posted December 12, 2011 Share Posted December 12, 2011 Here is a code that prints to the browser the name of the month plus the year with some modifications though. It is an if statement where it is judged first if we have reached the end of the year, in which case the next year is printed in the specific table cell: if(this.nextmonth==11){ var html = '<table class="calendar-table">'; html += '<tr><th colspan="7">'; html += monthName + " " +this.nextyear; html += '</th></tr>'; html += '<tr class="calendar-header">'; for(var i = 0; i <= 6; i++ ){ html += '<td class="calendar-header-day">'; html += cal_days_labels[i]; html += '</td>'; } html += '</tr><tr>';}else{ var html = '<table class="calendar-table">'; html += '<tr><th colspan="7">'; html += monthName + " " +this.year; html += '</th></tr>'; html += '<tr class="calendar-header">'; for(var i = 0; i <= 6; i++ ){ html += '<td class="calendar-header-day">'; html += cal_days_labels[i]; html += '</td>'; } html += '</tr><tr>';} The problem is that i get a message in the editor that the html variable is declared more than once. It would be an easy solution just change the name of the variable. The problem is though that in the end of the script(not shown here) i print/access the above code using return this.html(as another method) of the same object, in which object there is another method which contains the above code. In short. I have an object, whose one method constructs the html shown above and another method who outputs the code to the browser. How i should tackle the issue you think? Link to comment Share on other sites More sharing options...
justsomeguy Posted December 12, 2011 Share Posted December 12, 2011 That won't be a problem unless that code is part of a loop. But in general, you should declare all variables at the start of your code. The same thing goes for the variable i. Link to comment Share on other sites More sharing options...
jimfog Posted December 12, 2011 Author Share Posted December 12, 2011 The only loop here is the one that prints the names of the days. The rest of the code, as you see, it is not in a loop. So some other solution must be examined. Link to comment Share on other sites More sharing options...
Ingolme Posted December 12, 2011 Share Posted December 12, 2011 Declare it outside the conditional. var html;if( ... ) { html = ...} else { html = ...} Link to comment Share on other sites More sharing options...
jimfog Posted December 12, 2011 Author Share Posted December 12, 2011 Despite the fact i get no error this time-as i placed the variables outside the conditional, the problem is now that nothing gets printed to the browser. Here is the code(the revised one) again: var html;var i;if (this.nextmonth==11){ html = '<table class="calendar-table">'; html += '<tr><th colspan="7">'; html += monthName + " " +this.nextyear; html += '</th></tr>'; html += '<tr class="calendar-header">'; for( i = 0; i <= 6; i++ ){ html += '<td class="calendar-header-day">'; html += cal_days_labels[i]; html += '</td>'; } html += '</tr><tr>';} else{ // do the header(εδω τυπώνει τον μήνα και τις μέρες)html = '<table class="calendar-table">'; html += '<tr><th colspan="7">'; html += monthName + " " +this.year; html += '</th></tr>'; html += '<tr class="calendar-header">'; for( i = 0; i <= 6; i++ ){ html += '<td class="calendar-header-day">'; html += cal_days_labels[i]; html += '</td>'; } html += '</tr><tr>';} Go here to see the complete code: http://jszen.blogspot.com/2007/03/how-to-build-simple-calendar-with.html Link to comment Share on other sites More sharing options...
justsomeguy Posted December 12, 2011 Share Posted December 12, 2011 That code doesn't have anything where it's using the html variable to output. It's building the variable but not doing anything with it. But, again, that is only a problem if that code were in a loop. Your editor is using simple rules to check for errors and finds things like this which aren't actually errors. Still, it's good practice to declare variables at the start of the code. Link to comment Share on other sites More sharing options...
jimfog Posted December 12, 2011 Author Share Posted December 12, 2011 I GOT THE LITTLE S.......R. It was a logic error(VERY DIFFICULT YO PINPOINT). Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.