Reputation: 3495
I want javascript functions which take
integer
(week days[i.e 1,2,3...7 ]) and returnstring
(i.e 'Sunday','Monday'....) . but I dont know what are the best way to do this. it means what I should useenum
orarray
orswitch case
function GetFullName(weekDay) {
if (weekDay == 1)
return "Monday";
if (WeekDay = 2)
return "Tuesday";
}
But, I dont want to do like this......Is there any smart way to thi using create Enum
?
Upvotes: 0
Views: 3978
Reputation: 1127
try this
function GetFullName(weekDay)
{
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
var day = days[ weekDay-1];
return day;
}
Upvotes: 3
Reputation: 5493
Just another thought than array, create an object weekday with dayname and index:
var weekdays = {
Monday : 1,
Tuesday : 2,
Wednesday : 3,
Thursday:4,
Friday:5,
Saturday:6,
Sunday:7
}
function getFullName(weekDay) {
for( var prop in weekdays ) {
if( weekdays.hasOwnProperty( prop ) ) {
if( weekdays[ prop ] === weekDay )
return prop;
}
}
}
var fullName = getFullName(2);
console.log(fullName);
Upvotes: 2