Finding incorrectly marked rows for overdue books
Rows in red are supposed to represent overdue books.
HTML
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.3.3/semantic.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.3.3/semantic.js"></script>
<table class="ui table unstackable firstTable"><tbody>
<tr style="background-color: red">
<td>Donna</td>
<td>2016-10-01</td>
<td></td>
</tr>
<tr style="background-color: red">
<td>Nansen</td>
<td>2016-11-05</td>
<td>2016-11-14</td>
</tr>
<tr style="background-color: red">
<td>Peppe</td>
<td>2016-11-05</td>
<td>2016-11-14</td>
</tr>
<tr>
<td>J'Sira</td>
<td>2012-02-28</td>
<td>2012-03-14</td>
</tr>
<tr>
<td>Billie</td>
<td>2015-08-14</td>
<td>2015-10-09</td>
</tr>
<tr>
<td>Peyton</td>
<td>2015-08-14</td>
<td>2015-10-09</td>
</tr>
</tbody></table>
<br><br>
<table class="ui table unstackable secondTable"><tbody>
<tr>
<td>Addison</td>
<td>2014-08-14</td>
<td>2014-10-09</td>
</tr>
<tr>
<td>Val</td>
<td>2014-08-14</td>
<td>2014-10-09</td>
</tr>
<tr>
<td>Patrick</td>
<td>2015-11-23</td>
<td></td>
</tr>
</tbody></table>
JavaScript
function solution(D, T, whichTable) {
var table = $('table.' + whichTable), // REMOVE .this FOR TASK
howManyMarkedIncorrectly = 0;
table.find('tr').each(function() { // Search through each table row
var This = $(this),
dateBorrowed = new Date(This.children('td:nth-child(2)').text()),
dateReturned = This.children('td:nth-child(3)').text();
if (dateReturned) { // Check if book was returned
dateReturned = new Date(dateReturned);
}
var currentDate = new Date(D),
daysAllowedToHoldBook = T,
diff_fromCurrentDate = new Date(currentDate - dateBorrowed),
diff_fromReturnedDate = new Date(dateReturned - dateBorrowed),
overdue = false,
markedRed = false,
markedIncorrectly = false;
// Check if book was/is overdue
if (!dateReturned &&
convertIntoDays(diff_fromCurrentDate) > daysAllowedToHoldBook ||
convertIntoDays(diff_fromReturnedDate) > daysAllowedToHoldBook) {
overdue = true;
}
// Check if row was marked Red
if (This.css('background-color') == 'rgb(255, 0, 0)') {
markedRed = true;
}
// Check if row was marked correctly with/without Red
if (!overdue && markedRed ||
overdue && !markedRed) {
markedIncorrectly = true;
howManyMarkedIncorrectly++;
}
});
function convertIntoDays(num) {
return num/1000/60/60/24;
}
return howManyMarkedIncorrectly;
}
console.log(solution('2016-11-30', 14, 'firstTable'));
console.log(solution('2015-11-30', 7, 'secondTable'));