Jump to content

Variable Redeclaration Problem


jimfog

Recommended Posts

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

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

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

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

I GOT THE LITTLE S.......R. It was a logic error(VERY DIFFICULT YO PINPOINT).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...