Skip to content

Instantly share code, notes, and snippets.

@carlosvega20
Last active November 23, 2015 16:46
Show Gist options
  • Save carlosvega20/406abf4702aca4517668 to your computer and use it in GitHub Desktop.
Save carlosvega20/406abf4702aca4517668 to your computer and use it in GitHub Desktop.
//Simulation
var world = {gravity: 9.8, intervalTime: 100, verticalLimit: 600, dt: 0.1}
function simulation (obj) {
var t = 0, top = 0;
var interval = setInterval(function () {
if(top>world.verticalLimit) clearInterval(interval);
top += (world.gravity*obj.mass)*t;
obj.el.style.top = top+"px";
t += world.dt;
}, world.intervalTime);
}
document.body.style.position = 'relative';
new simulation({el: document.body, velocity: 0, mass:(Math.random() * 10)})
/////////////////////////////////////////////////////////////////////////////////////////
//change colors of every element on the page
function changeColors(el) {
var el = el || document.body;
Array.prototype.slice.call(el.childNodes).forEach(function(el, i) {
if (el.nodeType == Node.ELEMENT_NODE) {
el.style.backgroundColor = "#" + ((1 << 24) + (Math.round(Math.random() * 255 / 1) << 16) + (Math.round(Math.random() * 255 / 1) << 8) + Math.round(Math.random() * 255 / 1)).toString(16).slice(1);
}
if (el.childNodes.length > 0) {
changeColors(el)
}
});
}
changeColors();
//replace in a string based on regex
"this is an expample of how to use a custom text highlight".replace(/(is|ex)/g, function (arg) { return "<span class='highlight'>" + arg + "</span>"})
//"th<span class='highlight'>is</span> <span class='highlight'>is</span> an <span class='highlight'>ex</span>pample of how to use a custom t<span class='highlight'>ex</span>t highlight"
//hightlight bookmark
function hl(exp, attr, el) {
var el = el || document.body,
attr = attr || "style='background-color: yellow'",
nodes = el.childNodes;
for (var n=0; n<nodes.length; n++) {
if (nodes[n].nodeType == Node.TEXT_NODE) {
nodes[n].textContent = nodes[n].textContent.replace(exp, function (arg) {return "<span "+attr+">"+arg+"</span>"});
} else {
hl(exp, attr, nodes[n]);
}
}
}
//JSON
var json =
{'mylibs':
[
{'author':'carlos', 'rating':1, 'name':'twingo', 'available':false},
{'author':'andres', 'name':'ortensias', 'rating':5, 'available':false},
{'author':'cata', 'name':'tulipan', 'rating':3, 'available':true},
{'author':'andres', 'name':'amiga', 'rating':4, 'available':true}]}
//Array.forEach
json.mylibs.forEach(function (el, index){console.log(el,index)})
//group author by its name
var myObj = {}
json.mylibs.forEach(function (el,index){
if (!myObj[el.author]) {
myObj[el.author] = [];
}
myObj[el.author].push({'available': el.available, 'rating': el.rating, 'name': el.name})
})
//console.log(myObj)
//Array.sort
//sort from higher to lower (with Array.reverse)
json.mylibs.sort(function (a,b){return a.rating - b.rating}).reverse()
//"[{"author":"andres","name":"ortensias","rating":5,"available":false},{"author":"cata","name":"tulipan","rating":3,"available":true},{"author":"carlos","rating":1,"name":"twingo","available":false}]"
//Array.filter by availability
json.mylibs.filter(function (el,index){return el.available === true})
"[{"author":"cata","name":"tulipan","rating":3,"available":true},{"author":"andres","name":"amiga","rating":4,"available":true}]"
//Array.find
json.mylibs.find(function (el,index){return el.author === 'andres'})
// {author: "andres", name: "ortensias", rating: 5, available: false}
//Object for Each
var obj = {a:"one", b:"two"};
Object.keys(obj).forEach(k=>console.log(k+":"+obj[k]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment