the function calculates and returns how many hours (total) this person worked in the week based on the items in the given array in javascript

Below is an example function in JavaScript that calculates and returns the total hours worked in a week based on the items in the given array:

index.tsx
function calculateTotalHoursWorked(weeklySchedule) {
  let totalHours = 0;

  for (let i = 0; i < weeklySchedule.length; i++) {
    let dailySchedule = weeklySchedule[i];

    for (let j = 0; j < dailySchedule.length; j++) {
      let hoursWorked = dailySchedule[j];

      totalHours += hoursWorked;
    }
  }

  return totalHours;
}
335 chars
16 lines

The function takes in a parameter called weeklySchedule, which is an array of arrays representing the hours worked each day of the week. The function initializes a variable called totalHours to 0, and then loops through each day of the week and each hour worked on that day using a nested for loop. For each hour worked, it adds that value to the totalHours variable. Finally, the function returns the total hours worked in the week.

Here's an example usage of the function with sample data:

index.tsx
const weeklySchedule = [
  [8, 7, 8, 8, 6], // Monday
  [7, 8, 8, 6, 9], // Tuesday
  [8, 8, 8, 8, 7], // Wednesday
  [8, 8, 9, 7, 7], // Thursday
  [7, 7, 7, 7, 8], // Friday
  [0, 0, 0, 0, 0], // Saturday
  [0, 0, 0, 0, 0]  // Sunday
];

console.log(calculateTotalHoursWorked(weeklySchedule)); // Output: 186
311 chars
12 lines

In this example, the weeklySchedule array represents a typical work week with hours worked each day. The calculateTotalHoursWorked function is called with the weeklySchedule array as the argument, and it returns the total hours worked in the week, which is 186.

gistlibby LogSnag