简体   繁体   中英

How to set multiple task per hour?

I need to set multiple tasks per hour, but can't figure out how to do it. This is my code so far:

`

let daySchedule = [
  "08:00", 
  "09:00", 
  "10:00", 
  "11:00", 
  "12:00", 
  "13:00", 
  "14:00", 
  "15:00", 
  "16:00", 
  "17:00", 
  "18:00", 
  "19:00",
  "20:00"
];

let tasks = [1, 2, 3]; 

let taskDescriptions = [
  "Checking vegetables",
  "Baking bread",
  "Cleaning",
  "Free hour"
];

let temp= 0;

while(temp!=daySchedule.length){
     temp%tasks[0]===1 
     ? (console.log(daySchedule[temp] + " " + taskDescriptions[0])) 
     : (console.log(daySchedule[temp] + " " + taskDescriptions[3]))
     temp++;
}

`

The outcome should be: 08:00 - Checking vegetables 09:00 - Checking vegetables, Baking bread 10:00 - Checking vegetables, Baking bread, Cleaning

I used a while loop and modulo for tasks[0], but I can't figure out how to display the other tasks per hour?

Are you looking an object? That can store the tasks in the daySchedule . Then we only need to slice the entries to display the first 3 hours.

let daySchedule = {
  "08:00": [1],
  "09:00": [1, 2],
  "10:00": [1, 2, 3],
  "11:00": [],
  "12:00": [],
  "13:00": [],
  "14:00": [],
  "15:00": [],
  "16:00": [],
  "17:00": [],
  "18:00": [],
  "19:00": [],
  "20:00": []
};

let tasks = [1, 2, 3]; 

let taskDescriptions = [
  "Checking vegetables",
  "Baking bread",
  "Cleaning",
  "Free hour"
];

for (const [hour, tasks] of Object.entries(daySchedule).slice(0, 3)) {
  console.log(`${hour} - ${tasks.map(task => taskDescriptions[task - 1]).join(", ")}`);
}

Output:

08:00 - Checking vegetables
09:00 - Checking vegetables, Baking bread
10:00 - Checking vegetables, Baking bread, Cleaning

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM