Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

javascript if statement if is always true

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">&nbsp;</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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think it’s because you’re re-assigning the variable in your if statement
if(lastMonthDay = 31) vs if(lastMonthDay == 31)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading