views:

47

answers:

2

I have this code:

var str = $("#datepicker").datepicker("getDate");
    var datestr = str.toString().split(" ");
    switch(datestr[1]) {
        case "Jan":
        var datestrmon = "Яну";
        break;
        case "Feb":
        var datestrmon = "Фев";
        break;
        case "Mar":
        var datestrmon = "Мар";
        break;
        case "Apr":
        var datestrmon = "Апр";
        break;
        case "May":
        var datestrmon = "Май";
        break;
        case "Jun":
        var datestrmon = "Юни";
        break;
        case "July":
        var datestrmon = "Юли";
        break;
        case "Aug":
        var datestrmon = "Авг";
        break;
        case "Sep":
        var datestrmon = "Сеп";
        break;
        case "Oct":
        var datestrmon = "Окт";
        break;
        case "Nov":
        var datestrmon = "Ное";
        break;
        case "Dec":
        var datestrmon = "Дек";
        break;
        default:
        datestr[1];
    }
    alert(datestrmon);

I expect when the browser is alerting me datestrmon to alert "Юли" ,but it alerts me "undefined".Why?

+3  A: 

Try defining datestrmon outside the case. Below is some cleaned up code. If it doesn't work, please post your input.

var str = $("#datepicker").datepicker("getDate");
var datestr = str.toString().split(" ");
var datestrmon = "";

switch(datestr[1]) {
    case "Jan": {
     datestrmon = "Яну";
     break;
    }
    case "Feb": {
     datestrmon = "Фев";
     break;
    }
    case "Mar": {
     datestrmon = "Мар";
     break;
    }
    case "Apr": {
     datestrmon = "Апр";
     break;
    }
    case "May": {
     datestrmon = "Май";
     break;
    }
    case "Jun": {
     datestrmon = "Юни";
     break;
    }
    case "July": {
      datestrmon = "Юли";
      break;
    }
    case "Aug": {
     datestrmon = "Авг";
     break;
    }
    case "Sep": {
     datestrmon = "Сеп";
     break;
    }
    case "Oct": {
     datestrmon = "Окт";
     break;
    }
    case "Nov": {
     datestrmon = "Ное";
     break;
    }
    case "Dec": {
     datestrmon = "Дек";
     break;
    }
    default: {
     datestr[1];
    }
}
alert(datestrmon);
Kyle Rozendo
Now it doesn't alert me nothing
lam3r4370
Did you try with the above (with brackets?). If so, post your input here to we can have a look - it might be that the case is incorrect.
Kyle Rozendo
The above should work. Just to explain why briefly - when you put "var datestrmon" that determines the scope of the variable. Since you were declaring it inside the switch, as soon as the switch finished it went out of scope and ceased to exist. By putting "var datestrmon" outside of the switch, it has a wider scope.
Nick
@Nick: Absolutely wrong. `var` in Javascript is bound to the function, not the braces.
KennyTM
It is the same.
lam3r4370
KennyTM: you're absolutely right. I've upvoted your comment - I've been writing a lot of Java recently and must have had the wrong hat on.
Nick
+5  A: 
  1. Are you sure it's "July" instead of "Jul"?
  2. Your default: case did not assign datestr[1] to datestrmon. Is it intentional?

Anyway, consider using a lookup table instead of a big switch.

var datestrmon = ({"Jan": "Яну",
                   "Feb": "Фев",
                   ...
                   "Dec": "Дек"})[datestr[1]] || datestr[1];
//                                            ^^^^^^^^^^^^^
//                                            optional, if you think stuff outside of
//                                            the table could appear and need a
//                                            fallback.
KennyTM
This works like a charm.
lam3r4370