Object.defineProperties(Array.prototype, { count: { value: function (query) { /* Counts number of occurrences of query in array, an integer >= 0 Uses the javascript == notion of equality. */ var count = 0; for(let i = 0; i < this.length; i++) if(this[i] == query) count++; return count; } } }); const mean = arr => arr.reduce((p, c) => p + c, 0) / arr.length; tables = $(".table-div"); function generateExtra() { $(".auto-generated").remove(); global_shifts = new Set(); tables.each((n, table) => { rows = $(table).find(".worker-row"); workers = {}; rows.each((n, tr) => { worker_td = $(tr).find("td:first"); worker = worker_td.attr("data-worker"); shift_tds = $(tr).find(".rota-day"); shifts = [] shift_tds.each((n, td) => { shifts.push($(td).attr("data-shift")); }) shifts_unique = new Set(shifts); shifts_unique.delete(""); global_shifts = new Set([...global_shifts, ...shifts_unique]); weekends_worked = 0; weeks = shift_tds.length / 7; whole_weekends_worked = 0; for(var i = 1; i <= weeks; i++) { if($(tr).find(`[data-week='${i}'][data-day='Sat']`).attr("data-shift") != "" || $(tr).find(`[data-week='${i}'][data-day='Sun']`).attr("data-shift") != "") { weekends_worked = weekends_worked + 1; } if($(tr).find(`[data-week='${i}'][data-day='Sat']`).attr("data-shift") != "" && $(tr).find(`[data-week='${i}'][data-day='Sun']`).attr("data-shift") != "") { whole_weekends_worked = whole_weekends_worked + 1; } } shift_counts = {}; total_shifts = 0; days_lost = 0; shifts_unique.forEach(el => { shift_counts[el] = shifts.count(el); total_shifts = total_shifts + shifts.count(el); // why like this? if(el == "night_weekend") { whole_weekends_worked = whole_weekends_worked - shifts.count(el) / 3 } if($("#global-settings").length == 1) { n = $(`#${el}-tdl`).val(); // console.log("SHIT", `${el}-tdl`, n) if(!isNaN(n)) { days_lost = days_lost + shifts.count(el) * n; } } // // 5 days for weekday nights // if(el == "night_weekday") { days_lost = days_lost + shifts.count(el) / 4 * 5 } // // 4 days for weekend nights // if(el == "night_weekend") { // days_lost = days_lost + shifts.count(el) / 3 * 4 // //whole_weekends_worked = whole_weekends_worked - shifts.count(el) / 3 // } // // 0.5 for twilights // if(el.includes("twilight")) { // twilight_days_lost = shifts.count(el) / 2 // days_lost = days_lost + twilight_days_lost // } }); // Also 1 day for a whole weekend days_lost = days_lost + whole_weekends_worked //days_lost = days_lost + whole_weekends_worked; //console.log(shift_counts); nwds = worker_td.attr("data-nwds"); console.log(nwds); if(nwds != "None") { nwds.split(", ").forEach((nwd) => { console.log(worker, nwd, shift_tds); $(shift_tds).filter(`.${nwd}`).addClass("nwd"); }) } workers[worker] = { "site": worker_td.attr("data-site"), "nwds": worker_td.attr("data-nwds"), "fte": worker_td.attr("data-fte_adj"), "end_date": worker_td.attr("data-end_date"), "shifts": shifts, "shift_types": shifts_unique, "weekends_worked": weekends_worked, "shift_counts": shift_counts, "days_lost": days_lost, //"shifts" } sc = JSON.stringify(shift_counts); worker_td.after(`