eshellborn
eshellborn

Reputation: 11261

Javascript function with argument

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?

Upvotes: 1

Views: 129

Answers (7)

kmh
kmh

Reputation: 61

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

Upvotes: 0

Kerem
Kerem

Reputation: 11586

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

Upvotes: 0

Fenton
Fenton

Reputation: 251282

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.

Upvotes: 2

AMADANON Inc.
AMADANON Inc.

Reputation: 5929

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

Upvotes: 0

taylorc93
taylorc93

Reputation: 3716

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

Upvotes: 0

djechlin
djechlin

Reputation: 60848

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?

Upvotes: 5

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35983

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

Upvotes: 0

Related Questions