Skip to content

Instantly share code, notes, and snippets.

@shadowmint
Created July 18, 2014 03:33
Show Gist options
  • Save shadowmint/04774c0a2cde2e4c23e2 to your computer and use it in GitHub Desktop.
Save shadowmint/04774c0a2cde2e4c23e2 to your computer and use it in GitHub Desktop.
Javascript plays
// Fake FAaaake
var src = '123121312322211112222';
var offset = 0;
function next() {
var rtn = offset < src.length ? src[offset] : null;
offset += 1;
return rtn;
}
function score(l1) {
var score = 0;
for (var i = 0; i < l1.length; ++i) {
if (l1[i] == 1) score += 0.25;
if (l1[i] == 2) score += 0.5;
if (l1[i] == 3) score += 1;
}
return score;
}
function handle_thing(item) {
// [div, div, div]
// $(item[0]).parent() { ... }
if (item.length == 1) {
console.log('1 items: ' + item);
}
else if (item.length == 2) {
console.log('2 items: ' + item);
}
else if (item.length == 3) {
console.log('3 items: ' + item);
}
}
var list = [];
function doit(element) {
var next_item = parseInt($(element).attr('data-emph'));
// Add to the list
var tmp = list.slice();
tmp.push(next_item);
// Score for the current list
var value = score(tmp);
// If we have a whole row?
if (value == 1) {
handle_thing(tmp);
list = [];
tmp = [];
}
else if (value > 1) {
handle_thing(list);
list = [next_item];
}
else {
list.push(next_item);
}
}
}
$('.foo').each(function(i, element) {
doit(element);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment