Last active
February 6, 2016 21:07
-
-
Save abdulhannanali/a9f1ca7a12f424d4fcf9 to your computer and use it in GitHub Desktop.
Horrible JavaScript code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<tr class="student"> | |
<td class="name-col">Slappy the Frog</td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="missed-col">0</td> | |
</tr> | |
<tr class="student"> | |
<td class="name-col">Lilly the Lizard</td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="missed-col">0</td> | |
</tr> | |
<tr class="student"> | |
<td class="name-col">Paulrus the Walrus</td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="missed-col">0</td> | |
</tr> | |
<tr class="student"> | |
<td class="name-col">Gregory the Goat</td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="missed-col">0</td> | |
</tr> | |
<tr class="student"> | |
<td class="name-col">Adam the Anaconda</td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="attend-col"><input type="checkbox"></td> | |
<td class="missed-col">0</td> | |
</tr> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* STUDENTS IGNORE THIS FUNCTION | |
* All this does is create an initial | |
* attendance record if one is not found | |
* within localStorage. | |
*/ | |
(function() { | |
// even the localStorage initialization code here | |
// is dependent on the view for it's data | |
// that's totally horrible | |
// just create a simple array and take much of my data from there | |
// through it I can also initialize the localStorage | |
if (!localStorage.attendance) { | |
console.log('Creating attendance records...'); | |
function getRandom() { | |
return (Math.random() >= 0.5); | |
} | |
var nameColumns = $('tbody .name-col'), | |
attendance = {}; | |
nameColumns.each(function() { | |
var name = this.innerText; | |
attendance[name] = []; | |
for (var i = 0; i <= 11; i++) { | |
attendance[name].push(getRandom()); | |
} | |
}); | |
localStorage.attendance = JSON.stringify(attendance); | |
} | |
}()); | |
/* STUDENT APPLICATION */ | |
$(function() { | |
var attendance = JSON.parse(localStorage.attendance), | |
$allMissed = $('tbody .missed-col'), | |
$allCheckboxes = $('tbody input'); | |
// Count a student's missed days | |
// counting missed days using data from DOM. I haven't even written this bad | |
// code from the day one | |
function countMissing() { | |
$allMissed.each(function() { | |
var studentRow = $(this).parent('tr'), | |
dayChecks = $(studentRow).children('td').children('input'), | |
numMissed = 0; | |
dayChecks.each(function() { | |
if (!$(this).prop('checked')) { | |
numMissed++; | |
} | |
}); | |
$(this).text(numMissed); | |
}); | |
} | |
// Check boxes, based on attendace records | |
$.each(attendance, function(name, days) { | |
var studentRow = $('tbody .name-col:contains("' + name + '")').parent('tr'), | |
dayChecks = $(studentRow).children('.attend-col').children('input'); | |
dayChecks.each(function(i) { | |
$(this).prop('checked', days[i]); | |
}); | |
}); | |
// When a checkbox is clicked, update localStorage | |
// the whole model is created once again and that's horrible code | |
$allCheckboxes.on('click', function() { | |
var studentRows = $('tbody .student'), | |
newAttendance = {}; | |
studentRows.each(function() { | |
var name = $(this).children('.name-col').text(), | |
$allCheckboxes = $(this).children('td').children('input'); | |
newAttendance[name] = []; | |
$allCheckboxes.each(function() { | |
newAttendance[name].push($(this).prop('checked')); | |
}); | |
}); | |
countMissing(); | |
localStorage.attendance = JSON.stringify(newAttendance); | |
}); | |
countMissing(); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment