Question on conditionals-Active learning: A simple calendar

task page

show solution:

     let days = 31;
         if (choice === 'February') {
            days = 28;
          } else if (choice === 'April' || choice === 'June' || choice === 'September'|| choice === 'November') {
            days = 30;
          }

my work:

    if (choice === 'April' ||choice === 'June' || choice === 'September' || choice === 'November'){
        let days=30;
       } else if (choice==='February') {
        days=28;
       }  else {
        days=31;
       }

I can’t see the differnce, but the fact is my code just doesn’t work :sob:

The difference is where the let is. let days = 30; is only visible inside the if block. The other two days assignments create a global variable days because they have no let or const. Only this global variable can than be seen by the createCalendar() function
To make your version work you need to first declare your variable outside the if block:

let days;
if (choice === 'April' ||choice === 'June' || choice === 'September' || choice === 'November') {
  days=30;
} else if (choice==='February') {
  days=28;
} else {
  days=31;
}

Happy coding,
Michael

1 Like

wow , genius!! that is really cool!

1 Like