We help IT Professionals succeed at work.
Get Started

How to calculate cleaning turns if every flatmate is doing a two weeks turn dynamically?

ltpitt
ltpitt asked
on
97 Views
Last Modified: 2020-01-09
Hi all,

I am building a table that automatically shows who is the person that is in charge of cleaning the kitchen in plain Javascript.

This is what I have so far:
<style>
th, td {
  padding: 15px;
  text-align: left;
}

tr:hover {background-color: #f5f5f5;}

table {
  width: 100%;
}
</style>

<div id="cleaningTableDiv">
</div>


<script>

Date.prototype.getWeek = function() {
    var onejan = new Date(this.getFullYear(), 0, 1);
    return Math.ceil((((this - onejan) / 86400000) + onejan.getDay() + 1) / 7);
}

function plainTableCreate(currentWeekNumber) {
    // Table variables
    var teamMembers = ["Flatmate1", "Flatmate2", "Flatmate3", "Flatmate4", "Flatmate5", "Flatmate6"];
    var rowNumber = 7;
    var colNumber = teamMembers.length * 2 + 1;
    var headings = ["Week number"]
    for (var i = 0; i < colNumber - 1; i++) {
        headings.push(currentWeekNumber + i);
    }

    // Creating table
    var body = document.getElementById('cleaningTableDiv');
    var tbl = document.createElement('table');
    tbl.setAttribute("id", "cleaningTable");
    var tbdy = document.createElement('tbody');
    for (var i = 0; i < rowNumber; i++) {
        var tr = document.createElement('tr');
        for (var j = 0; j < colNumber; j++) {
            var td = document.createElement('td');
            td.appendChild(document.createTextNode(""));
            tr.appendChild(td);
        }
        tbdy.appendChild(tr);
    }
    tbl.appendChild(tbdy);
    body.appendChild(tbl);

    // Populating headings
    for (var i=0; i < headings.length; i++){
        document.getElementById("cleaningTable").rows[0].cells[i].innerHTML = headings[i];
    }

    // Populating 1st column with Team Members
    for (var i=1; i < rowNumber; i++){
        document.getElementById("cleaningTable").rows[i].cells[0].innerHTML = teamMembers[i-1];
    }

    // Populating X
    for (var i=1; i < rowNumber; i++){
        for (var j=1; j < colNumber; j++){
            if (i == 4) {
                // Flatmate 4 started cleaning week 1 and 2, next rotation will happen every 2
                // weeks, for example flatmate 5 will clean week 3 and 4, flatmate 6 will clean
                // week 5 and 6, flatmate 6 will clean week 7 and 8, flatmate 1 will clean
                // week 9 and 10 and so on...
                document.getElementById("cleaningTable").rows[i].cells[j].innerHTML = "X";                  
            }
            console.log(document.getElementById("cleaningTable").rows[0].cells[j].innerHTML);

        }
    }

}


var currentWeekNumber = (new Date()).getWeek();
plainTableCreate(currentWeekNumber);

</script>

Open in new window


I cannot think how to populate dynamically the turns, given the fact that the flatmate4 started cleaning week 1 and 2 of the year.

How can I be able to make the script perpetually work?

Thanks for your help.
Comment
Watch Question
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2017
Commented:
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE