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?

4
  • 1
    Have you heard about swtich? Commented Jul 2, 2013 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 .. Commented Jul 2, 2013 at 20:01
  • actually, an object is far better than switch when one input demands one output. Commented Jul 2, 2013 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 .. Commented Jul 2, 2013 at 20:03

7 Answers 7

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?

Sign up to request clarification or add additional context in comments.

7 Comments

What's a red herring? Is that food? :D
The code does not change a copy of a reference. A new value is assigned to the variable month.
@dbf en.wikipedia.org/wiki/Red_herring. It means you are investigating a plausible problem, but the real problem is somewhere else.
@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.
Ok, I'm really confused about all this pass by stuff. So wouldn't putting a return in fix everything?
|
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.

1 Comment

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.
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);

Comments

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;
}

1 Comment

sorry, forgot to add one thing. you have to store the returned value, so change getMonthName(orangemonth1); to orangemonth1 = getMonthName(orangemonth1);
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

2 Comments

month is not passed anywhere. orangemonth1 is passed.
I meant to say "the variable orangemonth1 is passed to month by value" - the rest still applies.
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));

Comments

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);

3 Comments

Arguments are passed by value. It's that simple.
You're right, they're passed by value, but to a newbie, it's not simple. 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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.