/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] switch statement

[Note: The icon for the post says resolved…it is not!]

Greetings,

I am implementing a switch statement, but am having a problem. For some reason, the first time that the switch evaluates the expression, it matches the correct case. However, every subsequent call always fails to the default case. The relevant code follows below(lots of alerts for debugging purposes). As an example, here’s the current problem. The first time this function gets called, it is passed the number 6. The alerts that pop up are “yay6” and “July”, respectively. However, the next call to the function passes it the number 5 and the alerts that pop up are “next popup = numberRep value”, “5”, and “null”, respectively. Any ideas as to why? I’m wondering if it has something to do with declaring the variable monthName each time…but yeah I’m not too sure. Any advice would be appreciated. Thanks in advance, and here’s the code:

function getMonthname( numberRep )
{
var monthName = ‘null’;
switch( numberRep ){
case 0: monthName = ‘January’; alert( “yay0” ); break;
case 1: monthName = ‘February’; alert( “yay1” ); break;
case 2: monthName = ‘March’; alert( “yay2” ); break;
case 3: monthName = ‘April’; alert( “yay3” ); break;
case 4: monthName = ‘May’; alert( “yay4” ); break;
case 5: monthName = ‘June’; alert( “yay5” ); break;
case 6: monthName = ‘July’; alert( “yay6” ); break;
case 7: monthName = ‘August’; alert( “yay7” ); break;
case 8: monthName = ‘September’; alert( “yay8” ); break;
case 9: monthName = ‘October’; alert( “yay9” ); break;
case 10: monthName = ‘November’; alert( “yay10” ); break;
case 11: monthName = ‘December’; alert( “yay11” ); break;
default: alert(“next popup = numberRep value”); alert( numberRep); break;
}
alert( “Returning the following value…”) ;
alert( monthName );
return monthName;
}

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @RyanSteele spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...