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.tsx335 chars16 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.tsx311 chars12 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