VadR
VadR

Reputation: 5

javascript is only recognizing my if Statement

so i have been looking a solution for this problem for days but i can't seem to find it.

the code basically works by getting the current day (eg: tueday) via moment.js then converting it to a string and then after that passing it to the if else statement.

but if you run the code the only thing that always appears is Day 68, no matter what the day is. below is the code itself.

var dayN = moment().format('dddd');
var dayC = JSON.stringify(dayN);
document.write(dayC);

function getSched() {
    if (dayC = "Monday") {
        document.getElementById('here').innerHTML = 'Day 68';
    } else if (dayC = "Tuesday") {
        document.getElementById('here').innerHTML = 'Day 2';
    } else if (dayC = "Wednesday") {
        document.getElementById('here').innerHTML = 'Day 3';
    } else if (dayC = "Thursday") {
        document.getElementById('here').innerHTML = 'Day 4';
    } else if (dayC = "Friday") {
        document.getElementById('here').innerHTML = 'Day 5';
    } else if (dayC = "Saturday") {
        document.getElementById('here').innerHTML = 'Day 6';
    } else if (dayC = "Sunday") {
        document.getElementById('here').innerHTML = 'Day 7';
    } else {
        document.getElementById('here').innerHTML = 'GG WP';
    }
}

Upvotes: 0

Views: 53

Answers (2)

Mimouni
Mimouni

Reputation: 3640

I correct the code with some improvements :

the use of a 'switch' can clarify the code.

function getSched( dayC ) {  // declaration of the function 
    var val='';
    switch( dayC ){
         case "Monday":
              val ='Day 68';
              break;
         case "Tuesday":
              val ='Day 2';
              break;
         case "Wednesday":
              val ='Day 3';
              break;
         case "Thursday":
              val ='Day 4';
              break;
         case "Friday":
              val ='Day 5';
              break;
         case "Saturday":
              val ='Day 6';
              break;
         case "Sunday":
              val ='Day 7';
              break;
         default:
              val ='GG WP';
              break;
    }

    return val;
}


var dayN = moment().format('dddd');
var dayC = JSON.stringify(dayN);
document.getElementById('here').innerHTML = getSched( dayC );

try document.getElementById() instead of document.write it's not recommended to use.

Upvotes: 0

alex
alex

Reputation: 490283

You need to use double equal signs (==), otherwise you're doing truthy assignments.

Upvotes: 4

Related Questions