usn-fizz-better fixed re
by brico
HTML
<div role="main">
<table caption="Fizz Buzz" summary="A table of values, with Fizzes and Buzzes marked" class="fizzer">
<caption>Fizz Buzz</caption>
<thead>
<tr>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr><td>1</td></tr>
<tr><td>2</td></tr>
<tr><td>3</td></tr>
<tr><td>4</td></tr>
<tr><td>5</td></tr>
<tr><td>6</td></tr>
<tr><td>7</td></tr>
<tr><td>8</td></tr>
<tr><td>9</td></tr>
<tr><td>10</td></tr>
<tr><td>11</td></tr>
<tr><td>12</td></tr>
<tr><td>13</td></tr>
<tr><td>14</td></tr>
<tr><td>15</td></tr>
<tr><td>16</td></tr>
<tr><td>20</td></tr>
<tr><td>24</td></tr>
<tr><td>28</td></tr>
<tr><td>36</td></tr>
<tr><td>44</td></tr>
<tr><td>99</td></tr>
<tr><td>140</td></tr>
<tr><td>1260</td></tr>
</tbody>
</table>
</div>
JavaScript
(function($) {
$.fn.fizzbuzzify = function(options) {
// default settings give us the conventional plain jane game
var settings = $.extend({
statuses: {
'Fizz': 3,
'Buzz': 5
},
useDivisibleRule: true,
useDigitsRule: false,
addClass: false,
header: 'Status'
}, options);
return this.each(function() {
// as implemented, fizzbuzzify only works on a table
if (this.tagName.toLowerCase() == 'table') {
// add a header for the new column
$(this).find('tr th:first-child').after('<th>' + settings.header + '</th>');
$(this).find('tr td:first-child').each(function() {
var statusString = '',
value = $(this).text();
$.each(settings.statuses, function(k, v) {
// divisible rule -- the normal game
if (settings.useDivisibleRule === true) {
if (value % v === 0) {
statusString += k;
}
}
// digits rule -- the bonus round
if (settings.useDigitsRule === true) {
var re = new RegExp(v, 'g');
matches = value.toString().match(re);
//alert(matches);
if (matches !== null) {
$.each(matches, function() {
statusString += k;
});
}
}
});
// all those other uncool values
if (statusString === '') {
statusString = 'Uncool';
}
if...