Last active
August 29, 2015 14:06
-
-
Save bmoren/b40735390bc99cee6266 to your computer and use it in GitHub Desktop.
Generative JS Snippets
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
//set an an amount to loop through (this is often an array called with array.length) | |
// the for loop, set a varioable called 'i' to 0, as long as 'i' is less than the variable amount, then increase 'i' by after it executes the code in the curly braces each time | |
for (var i=0; i<amount; i++){ | |
//do stuff here | |
//do something each time! | |
console.log("this is iteration #" + i + " through the for loop"); | |
// example to append a thing 10 times | |
$('.theCount').append("<p class='underline'>iteration #" + i + "</p>"); | |
}; |
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
/*`~`~`~` A timer to call a function every second (1000ms) every second `~`~`~`*/ | |
setInterval(function () { | |
//do some stuff every second | |
console.log('this happens every second'); | |
}, 1000); |
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
var checkMe = 0; | |
// IF something is or is not equal to less than aor greater than, etc. THEN do something! | |
if (checkMe == 0){ | |
//do something if the above statement is TRUE | |
console.log('this statement is true') | |
$('.logicCheck').html("the checkMe variable is set to: " + checkMe + ", which means the 'if then' statement is TRUE!"); | |
} |
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
//Get me a random number in the specified range the specified amount of times...... | |
var iterations = 10; | |
var randRange = 100; | |
//set an an amount to loop through (this is often an array called with array.length) | |
// the for loop, set a varioable called 'i' to 0, as long as 'i' is less than the variable amount, then increase 'i' by after it executes the code in the curly braces each time | |
for (var i=0; i<iterations; i++){ | |
//Get a random number in a range | |
var getRand = Math.floor( Math.random() * randRange +1 ); | |
// example to append a rand number in a range the specified amount of times. | |
$('.forRand').append("<p class='yellow'>"+ getRand +"</p>"); | |
}; | |
} |
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
//functions needed to make it happen (I didnt write these but I use them all the time) | |
function range(start, end) { | |
var foo = []; | |
for (var i = start; i <= end; i++) { | |
foo.push(i); | |
} | |
return foo; | |
} | |
function shuffle(o){ //v1.0 - google labs | |
for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); | |
return o; | |
} | |
//Do all of our stuff | |
function nonRepRand(lowrange, highrange){ | |
//generate an array with a range of numbers, this is to be shuffled which will give us our non-repeting-random # | |
var foo = range(lowrange, highrange); | |
console.log("foo:", foo); | |
//mix up the array of numbers in a range from 0 to totalTaskNum | |
shuffle(foo); | |
//finally, just get the numbers of the foo array 'in order' and render it to the screen | |
// for loop through the array using the calculated length (foo.length) | |
for (var i=0; i<foo.length; i++){ | |
//iterate through the array using the 'i' in the for loop, this will give us a new rand number each time but never the same number | |
var getNonRepRand = foo[i] ; | |
console.log(getNonRepRand); | |
// example to append a rand number in a range the specified amount of times. | |
$('.nonRepRand').append("<p class='red'>"+ getNonRepRand +"</p>"); | |
console.log(getNonRepRand); | |
}; | |
}; |
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
//is this thing working? | |
//console.log("yes, it is working!"); | |
//get a randm number in a range between 1 and our total number of elements | |
var getRand = Math.floor( Math.random() * amount +1 ); | |
console.log("the getRand variable is now set to: " + getRand); | |
$('.randDiv').html("the getRand variable is now set to: " + getRand); | |
//`~`~`~` This is an example of how to switch out a HTML5 video element using files stored in a folder that have numbered filenames`~`~`~` | |
// set the scr attribute to a new file based on the getRand random number generator above. | |
$('#video').attr("src", 'data/' + getRand + .mp4).get(0).play(); | |
// logs to the console the file that will play | |
console.log('data/' + getRand + .mp4) | |
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
// set the random range | |
var TLrandRange = 100 | |
// start a timer | |
setInterval(function () { | |
//get a random number on each iteration | |
var TLgetRand = Math.floor( Math.random() * TLrandRange +1 ); | |
//check logic against the random number, if its above 50 then do the text swap | |
if ( TLgetRand > 50){ | |
//push this text to the timerLogic div | |
$('.timerLogic').html("the random number that is generated each seconds is ABOVE 50, in fact it's: " + TLgetRand ); | |
// else, means if the original conditions are not met, then do this instead, so in this case, if its not greater than 50, do the text swap below! | |
} else{ | |
//push this text to the timerLogic div | |
$('.timerLogic').html("the random number that is generated each seconds is BELOW 50, in fact it's: " + TLgetRand ); | |
} | |
// how often to iterate | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment