I am making a calendar and the if statement is supposed to see if the last day of the month is 30, 31, or anything else. When I test the code, the statement always affects the if statement even when the condition is not true. Here is my html code: ( all of this is correst)
<!DOCTYPE html>
<html>
<head>
<title>Calendar</title>
<link rel="stylesheet" href="calendar.css">
<script src="calendar.js"></script>
</head>
<body>
<main>
<h1><span id="month_year"> </span></h1>
<table id="calendar">
<tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th>
<th>Sat</th></tr>
</table>
</main>
</body>
</html>
For my javascript, I have set the last day of the month to be 30 to test the else if. (lastMonthDay = 30) here is my JavaScript code: var $ = function (id) { return document.getElementById(id); };
window.onload = function () {
var d = new Date();
var month = d.getMonth();
var year = new Date().getFullYear();
$("month_year").innerHTML = getMonthText(month) " " year;
var lastMonthDay = 30;
var calendarNum = 1;
var calendarDate = 1;
var tbodyRef = document.getElementById("calendar").getElementsByTagName("tbody")[0];
if(lastMonthDay = 31){
for ( i = 0; i < 5; i){
newRow = tbodyRef.insertRow();
for ( n = 0; n < 7; n){
newCell = newRow.insertCell()
if(calendarNum != 35 && calendarNum != 34
&& calendarNum != 1 && calendarNum != 2 ){
var newText = document.createTextNode(calendarDate);
newCell.appendChild(newText);
calendarDate = calendarDate 1;
}
else{
var newText = document.createTextNode("");
newCell.appendChild(newText);
}
calendarNum = calendarNum 1;
}
}
}
else if(lastMonthDay = 30){
for ( i = 0; i < 5; i){
newRow = tbodyRef.insertRow();
for ( n = 0; n < 7; n){
newCell = newRow.insertCell()
if(calendarNum != 35 && calendarNum != 34 && calendarNum != 33
&& calendarNum != 1 && calendarNum != 2 ){
var newText = document.createTextNode(calendarDate);
newCell.appendChild(newText);
calendarDate = calendarDate 1;
}
else{
var newText = document.createTextNode("");
newCell.appendChild(newText);
}
calendarNum = calendarNum 1;
}
}
}
else{
for ( i = 0; i < 5; i){
newRow = tbodyRef.insertRow();
for ( n = 0; n < 7; n){
newCell = newRow.insertCell()
if(calendarNum != 35 && calendarNum != 34 && calendarNum != 33 &&
calendarNum != 34
&& calendarNum != 1 && calendarNum != 2 ){
var newText = document.createTextNode(calendarDate);
newCell.appendChild(newText);
calendarDate = calendarDate 1;
}
else{
var newText = document.createTextNode("");
newCell.appendChild(newText);
}
calendarNum = calendarNum 1;
}
}
}
};
Any explanation or link to a similar question is appreciated. The only code that does not work is the if statements.
CodePudding user response:
The problem is, you are assigning LastMonthday with a number. You should use == or ===
If we look at it from good code writing practises, we should never use ==. Only use ===, because then you don't leave place for type errors that might occur.
LastMonthDay === 30
should do the trick
CodePudding user response:
I think it's because you're re-assigning the variable in your if statement
if(lastMonthDay = 31)
vs if(lastMonthDay == 31)