Last active
August 29, 2015 14:02
-
-
Save willmendesneto/a2fe1e052327ba5b5dee to your computer and use it in GitHub Desktop.
World Cup in six lines of Javascript based in @fmasanori jogos.py. Zepto/Jquery dependency
This file contains hidden or 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
// Example in http://codepen.io/willmendesneto/pen/Iguor | |
$.get('http://worldcup.sfg.io/matches', function(data){ | |
data.forEach(function(item){ | |
if (item.status === 'completed') { | |
console.log(item.home_team.country + ' ' + item.home_team.goals + ' x ' + item.away_team.country + ' ' + item.away_team.goals); | |
} | |
}); | |
}, 'json'); |
This file contains hidden or 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
(function(window, $){ | |
'use strict'; | |
// Example in http://codepen.io/willmendesneto/pen/jaDuC | |
// Using some things of functional javascript | |
$.get('http://worldcup.sfg.io/matches', function(data){ | |
var completePlays = []; | |
data.filter(function(item, i){ | |
if (item.status === 'completed') { | |
return item; | |
} | |
}, []).sort(function(a,b){ | |
return new Date(a.datetime) - new Date(b.datetime); | |
}).forEach(function(item, i){ | |
completePlays.push(item.location + ': ' + item.home_team.country + ' ' + item.home_team.goals + ' x ' + item.away_team.country + ' ' + item.away_team.goals ); | |
}); | |
console.log(completePlays.join('')); | |
}); | |
})(window, window.Zepto || window.jQuery); |
This file contains hidden or 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
(function(window, $){ | |
'use strict'; | |
// Example in http://codepen.io/willmendesneto/pen/zBJae | |
// Using some things of Javascript OOP | |
function WorldCup(){ | |
this.completePlays = []; | |
this.url = 'http://worldcup.sfg.io/matches'; | |
} | |
WorldCup.prototype.loadData = function() { | |
var self = this | |
$.get(this.url, function(data){ | |
data = self.filterData(data); | |
self.listsPlays(data); | |
console.log(self.completePlays.join('\n')); | |
}); | |
}; | |
WorldCup.prototype.filterByCompletePlays = function(data) { | |
return data.filter(function(item, i){ | |
if (item.status === 'completed') { | |
return item; | |
} | |
}, []); | |
}; | |
WorldCup.prototype.orderByDatetime = function(data) { | |
return data.sort(function(a,b){ | |
return new Date(a.datetime) - new Date(b.datetime); | |
}); | |
}; | |
WorldCup.prototype.listsPlays = function(data) { | |
var self = this; | |
data.forEach(function(item, i){ | |
self.completePlays.push(item.location + ': ' + item.home_team.country + ' ' + item.home_team.goals + ' x ' + item.away_team.country + ' ' + item.away_team.goals ); | |
}); | |
}; | |
WorldCup.prototype.filterData = function(data) { | |
return this.orderByDatetime(this.filterByCompletePlays(data)); | |
}; | |
var WorldCup = new WorldCup(); | |
WorldCup.loadData(); | |
})(window, window.Zepto || window.jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if Zepto doesn't existst, then use jQuery