1

I have two variables that are strings: month1Digit1 and month1Digit2. Together they make up a the decimal digits of a month (01-12), so month1Digit1 is always either 0 or 1, and month1Digit2 could be any number other than 0. Now I have several of these, as in month2Digit1, etc. I want a function that can determine the name of the month from these variables. But I don't want to write a separate function for each group just because it has different variables. From searching around it looks like I need to do a function with arguments, but I'm not really sure how this works. I tried the following:

var month1Digit1 = "1";
var month1Digit2 = "2";

function getMonthName (month) {
    if (month == "1") { month = "January" }
    else if (month == "2") { month = "February" }
    else if (month == "3") { month = "March" }
    else if (month == "4") { month = "April" }
    else if (month == "5") { month = "May" }
    else if (month == "6") { month = "June" }
    else if (month == "7") { month = "July" }
    else if (month == "8") { month = "August" }
    else if (month == "9") { month = "September" }
    else if (month == "10") { month = "October" }
    else if (month == "11") { month = "November" }
    else if (month == "12") { month = "December" }
}

var orangemonth1 = month1Digit1 + month1Digit2;
getMonthName(orangemonth1);
orangedate = orangemonth1;

Now from this, the value of orangedate should be 'December', no? But when I run this, I get "12" as the value. So the function isn't working. What am I doing wrong?

eshellborn
  • 11,031
  • 5
  • 20
  • 29
  • 1
    Have you heard about [`swtich`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch)? – Yotam Omer Jul 02 '13 at 20:01
  • At the end of that function, just return the `month` value and catch that result.. And please don't ask how to do that .. – dbf Jul 02 '13 at 20:01
  • actually, an object is far better than switch when one input demands one output. – dandavis Jul 02 '13 at 20:02
  • 2
    @YotamOmar Which is basically the same and the same kind of time consuming, better would be to use an array that holds the value from index zero and just decrement the given `month` argument .. – dbf Jul 02 '13 at 20:03

7 Answers7

5

You've caught a red herring.

function foo(in) {
    in = 2;
}
var a = 1;
foo(a);
console.log(a); // prints 1

Your problem is nowhere near or related to string concatenation; that you understand properly. You need to understand that Javascript is pass by copy of reference and you are changing a copy of a reference. See here: Is JavaScript a pass-by-reference or pass-by-value language?

Community
  • 1
  • 1
djechlin
  • 59,258
  • 35
  • 162
  • 290
  • What's a red herring? Is that food? :D – dbf Jul 02 '13 at 20:06
  • 1
    The code does not change a copy of a reference. A new value is assigned to the variable `month`. – a better oliver Jul 02 '13 at 20:12
  • @dbf https://en.wikipedia.org/wiki/Red_herring. It means you are investigating a plausible problem, but the real problem is somewhere else. – djechlin Jul 02 '13 at 20:14
  • @zeroflagL variable `month` is created by creating a reference to the input variable, which I guess is a copy if a reference is passed. All explained much better in linked question. – djechlin Jul 02 '13 at 20:22
  • Ok, I'm really confused about all this pass by stuff. So wouldn't putting a return in fix everything? – eshellborn Jul 02 '13 at 20:28
  • The variable `month` is created and a value is assigned to it. Within the function a new value is assigned to it. It doesn't matter what the value is (reference, copy, whatever). – a better oliver Jul 02 '13 at 20:29
  • @eshellborn pretty much, but you might want to try understanding "all this pass by stuff." You're going to be confused until you do. It's very fundamental and important. – djechlin Jul 02 '13 at 20:33
2

I would use an array to map the months to the numbering system you use and return the result from the function:

var month1Digit1 = '0';
var month1Digit2 = '7';

var monthMap = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

function getMonthName (input) {
    var monthNumber = parseInt(input);
    return monthMap[monthNumber];
}

var orangemonth1 = month1Digit1 + month1Digit2;
orangedate = getMonthName(orangemonth1);

alert(orangedate);

Demo on JSFiddle.

Fenton
  • 241,084
  • 71
  • 387
  • 401
  • 1
    As much as I know I should really learn what's going on here with the pass by stuff, this is the only answer that actually works right now, so thank you. – eshellborn Jul 02 '13 at 20:52
0

The problem is that you don't return any variable and not assign it.
The value change only into the function in your case.
try this:

var month1Digit1 = "1";
var month1Digit2 = "2";

function getMonthName (month) {
    if (month == "1") { month = "January" }
    else if (month == "2") { month = "February" }
    else if (month == "3") { month = "March" }
    else if (month == "4") { month = "April" }
    else if (month == "5") { month = "May" }
    else if (month == "6") { month = "June" }
    else if (month == "7") { month = "July" }
    else if (month == "8") { month = "August" }
    else if (month == "9") { month = "September" }
    else if (month == "10") { month = "October" }
    else if (month == "11") { month = "November" }
    else if (month == "12") { month = "December" }

    return (month);
}

var orangemonth1 = month1Digit1 + month1Digit2;
orangedate = getMonthName(orangemonth1);
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
0

You aren't returning anything from your function.

function getMonthName (month) {
    if (month == "1") { month = "January" }
    else if (month == "2") { month = "February" }
    else if (month == "3") { month = "March" }
    else if (month == "4") { month = "April" }
    else if (month == "5") { month = "May" }
    else if (month == "6") { month = "June" }
    else if (month == "7") { month = "July" }
    else if (month == "8") { month = "August" }
    else if (month == "9") { month = "September" }
    else if (month == "10") { month = "October" }
    else if (month == "11") { month = "November" }
    else if (month == "12") { month = "December" }
    return month;
}
taylorc93
  • 3,676
  • 2
  • 20
  • 34
  • sorry, forgot to add one thing. you have to store the returned value, so change `getMonthName(orangemonth1);` to `orangemonth1 = getMonthName(orangemonth1);` – taylorc93 Jul 02 '13 at 20:31
0

the variable month is passed by value - that is, when you call getMonthName, month is a COPY of orangemonth1, not the variable itself. So when you change month, you are not effecting orangemnoth.

Try adding return month to the end of getMonthName

AMADANON Inc.
  • 5,753
  • 21
  • 31
0

You can try this;

function getMonthName(month) {
    var months = ["", "January", "February", "March", "April", "May", "June", 
                  "July", "August", "September", "October", "November", "December"];
    // or
    var months = {1:"January", 2:"February", 3:"March", 4:"April", 5:"May", 6:"June", 
                  7:"July", 8:"August", 9:"September", 10:"October", 11:"November", 12:"December"};

    return months[parseInt(month)];
}

console.log(getMonthName(1));
Kerem
  • 11,377
  • 5
  • 59
  • 58
0

I think you're missing the problem of whether variables are passed into a function in javascript by reference or by value. The answer is not quite so straightforward as one might think. Check out this question.

Is JavaScript a pass-by-reference or pass-by-value language?

At any rate, if you want to make this work, instead of assuming your variable has been altered within the function, explicitly return your new value instead of month

function getMonthName (month) { if (month == "1") { month = "January" }... etc etc return month; }

Thereafter, you'll need to call your function like so:

var textMonth = getMonthName(orangemonth1);

Community
  • 1
  • 1
kmh
  • 61
  • 7
  • Arguments are passed by value. It's that simple. – a better oliver Jul 02 '13 at 20:49
  • You're right, they're passed by value, but to a newbie, it's not simple. http://stackoverflow.com/a/3638034/2533102 Try the sample code in this answer and you'll see why I said read this particular article. The item that is passed by value is itself a reference, so there are interesting, and to newbie, potentially unexpected, side effects. – kmh Jul 02 '13 at 22:53
  • IMHO it's not about noobs. Modern languages do their best to hide pointers from their. So many users of modern languages are not aware of the fact that variables contain references and not the actual objects. And if people have never worked with a language that supports pass-by-reference, chances are high that they don't know what it actually means. – a better oliver Jul 03 '13 at 05:59