I have an array of boolean values that represent whether a store is open on each day of the week.
Here are some test cases:
Case 1:
Input data: [true, true, true, true, true, true, true]
Expected output: Open every day
Case 2:
Input data: [true, true, true, true, true, false, false]
Expected output: Monday to Friday
Case 3:
Input data: [true, true, false, false, true, true, true]
Expected output: Monday, Tuesday, Friday to Sunday
Case 4:
Input data: [true, false, false, true, false, false, true]
Expected output: Monday, Thursday, Sunday
Case 5:
Input data: [true, true, false, true, true, true, false]
Expected output: Monday, Tuesday, Thursday to Saturday
Case 6:
Input data: [true, false, false, false, false, false, false]
Expected output: Only open on Monday
I've created a solution here, but need help with cases 2-5
const daysLabels = [
{ label: "Monday", short: "Mon" },
{ label: "Tuesday", short: "Tue" },
{ label: "Wednesday", short: "Wed" },
{ label: "Thursday", short: "Thu" },
{ label: "Friday", short: "Fri" },
{ label: "Saturday", short: "Sat" },
{ label: "Sunday", short: "Sun" }
];
const getSchedule = ({ case: days }) => {
let activeDays = [];
for (let i = 0; i < [...days].length; i++) {
const day = [...days][i];
if (day) {
activeDays.push({ ...daysLabels[i], value: day });
}
}
if (activeDays.length === 7) {
return "Open every day";
}
if (activeDays.length === 1) {
return `Only open on ${activeDays[0].label}`;
}
return "#TODO";
};
Sandbox - link