# Help with switches

I have a problem with a script.
a piece of it goes like this:

10 or 11 = “fall leaves”
12 or 1 = “snow”
4 or 5 = “rain”

Code:

var d = new Date();
var curr_month = d.getMonth();
if var curr_month == 1 then do function()

the function is snow.
i cant get to rain or leaves yet but i know they should work.
but the switch im using doesn’t work and i cant pin down the problem.
can someone help me?

if var curr_month== 1
using the keyword var redefines curr_month.

Javascript if statements need to be enclosed in parentheses.

``````var d= new Date();
var curr_month= d.getMonth();
if(curr_month== 1) return // something
``````

This is not a switch, it is an if statement.

A switch looks like:

``````var d= new Date();
var curr_month= d.getMonth();
switch(curr_month){
case 0: case 1: case: 11: return 'winter';
case 2: case 3: case 4:  return 'spring';
default: return 'summer and fall';
}

``````

Months are 0 based. january is 0, december is 11.There is no 12.

The switch statement is more appropriately structured as:

``````
var d= new Date(),
curr_month = d.getMonth();
switch (curr_month) {
case 0:
// pass through
case 1:
// pass through
case 11:
return 'winter';
break;
case 2:
// pass through
case 3:
// pass through
case 4:
return 'spring';
default:
return 'summer and fall';
}

``````

Although it may be clearer were you you create a season value from the current month.
You can take the 0-11 range, add 1 to it so it becomes a 1-12 range, then divide by 3 to get a season from 0 to 3. Finally, we do modulus with 4 to ensure that we stay within our range of 4 seasons.

``````
var d = new Date(),
curr_month = d.getMonth(),
season = Math.floor((curr_month + 1) / 3) &#37; 4;
switch (season) {
case 0:
return 'winter';
break;
case 1:
return 'spring';
break;
default:
return 'summer and fall';
}

``````