Home > Blockchain >  How to reduce if statements when the input parameter is array?
How to reduce if statements when the input parameter is array?

Time:01-05

I have a task to do where i have an array of days. I will have to print if the array includes specific days.The array can have any days in it as the user will decide what are the days they want to print.

 callDays(){
   
        const days: string[]=['Sunday','Monday','Wednesday','Thursday','Friday','Saturday'];
        if(days.includes('Sunday')){
        console.log('Sunday is included');
        }
        if(days.includes('Monday')){
        console.log('Monday is included');
        }
        if(days.includes('Tuesday')){
        console.log('Tuesday is included');
        }
        if(days.includes('Wednesday')){
        console.log('Wednesday is included');
        }
        if(days.includes('Thursday')){
        console.log('Thursday is included');
        }
        if(days.includes('Friday')){
        console.log('Friday is included');
        }
        if(days.includes('Saturday')){
        console.log('Saturday is included');
        }
     }

In the above case Tuesday shouldn't get printed as it is not available in the array. To do this task i had to use so many if statements. Is there a way i can do it without using this many if statements. Thank you.

CodePudding user response:

You probably want something like this:

function callDays(days: string[]) {
   const POSSIBLE_DAYS: string[] = ['Sunday','Monday','Wednesday','Thursday','Friday','Saturday'];

   days.forEach((day) => {
      if (POSSIBLE_DAYS.includes(day))
         console.log(`${day} is included`);
   });
}

A slightly more complicated implementation but will likely be faster is:

function callDays(days: string[]) {
   const POSSIBLE_DAYS_SET: Set<string> = new Set(['Sunday','Monday','Wednesday','Thursday','Friday','Saturday']);

   days.forEach((day) => {
      if (POSSIBLE_DAYS.has(day))
         console.log(`${day} is included`);
   });
}

Edit: For calling functions based off days in provided array

function sundayActivity() {}
function mondayActivity() {}
function tuesdayActivity() {}
function wednesdayActivity() {}
function thursdayActivity() {}
function fridayActivity() {}
function saturdayActivity() {}

const POSSIBLE_DAYS = {
  sunday: sundayActivity,
  monday: mondayActivity,
  tuesday: tuesdayActivity,
  wednesday: wednesdayActivity,
  thursday: thursdayActivity,
  friday: fridayActivity,
  saturday: saturdayActivity,
} as const

function callDays(days: string[]) {
  days.forEach((day) => {
    if (day.toLowerCase() in POSSIBLE_DAYS) 
      POSSIBLE_DAYS[day.toLowerCase()]()
  })
}

CodePudding user response:

You can do it by iterating through a days array like this:

const daysObj = {
    "monday": "Monday",
    "tuesday": "Tuesday",
    'wednesday':'Wednesday',
    'thursday': 'Thursday',
    'friday': 'Friday',
    'saturday':'Saturday',
    "sunday": "Sunday"
}

days.forEach(day =>{
    daysObj[day.toLowerCase()] && console.log(`${day} is included`)
})
  • Related