Skip to content

Instantly share code, notes, and snippets.

View caleboleary's full-sized avatar

Caleb O'Leary caleboleary

View GitHub Profile
function veryPresidential(data) {
var lines = data.split('|');
var presidents = [];
for (var i = 1; i < lines.length; i++) {
var values = lines[i].split(',');
presidents.push(
{
name:values[0].trim(),
birth_date:values[1].trim(),
birth_timestamp:Date.parse(values[1].trim())/1000,
<mvt:comment>Modulo/Modulus in Miva (get remainder of a/b)</mvt:comment>
<mvt:comment>in JavaScript '%' ex: 124%3 = 1 (3 goes in to 124 41 times, with 1 left over)</mvt:comment>
<mvt:comment>useful for deciding whether a number is evenly divisible by another, so if X%2 == 0, the number is even. if it isn't, its odd.</mvt:comment>
<mvt:assign name="g.A" value="124" />
<mvt:assign name="g.B" value="3" />
<mvt:assign name="g.mod" value="g.A - ( floor( (g.A / g.B) ) *g.B)" />
&mvt:global:mod;
@caleboleary
caleboleary / capitalize.html
Created January 25, 2016 17:48
miva capitalize first letter in string http://snippets.miva.com/id/1453743986/2
<mvt:assign name="g.str" value="'caleb'" />
<mvt:assign name="g.capitalize" value="toupper(substring(g.str,0,1)) $ subString(g.str,2,len(g.str))" />
&mvt:global:capitalize;
@caleboleary
caleboleary / highestCount.js
Created January 22, 2016 21:02
javascript most frequent in array
function highestCount(arr) {
var winnerCount = 0;
var winner = 0;
for (var i = 0; i < arr.length; i++) {
var count = 0;
for (var j = 0; j < arr.length; j++) {
if (j != i && arr[j] == arr[i]) {
count++;
}
};
@caleboleary
caleboleary / dailyprogrammer250int_BRUTEFORCE.js
Last active January 21, 2016 20:39
dailyprogrammer 250 (intermediate) SOLVED find self describing numbers of x digits https://www.reddit.com/r/dailyprogrammer/comments/41tdzy/20160120_challenge_250_intermediate/
//console.time('time');
//describe an integer
function describe(int) {
intSplit = String(int).split('');
var descriptionHolder = [];
for (var i = 0; i < intSplit.length; i++) {
var count = intSplit.reduce(function(n, val) {
return n + (val == i);
}, 0);
var targetString = 'Hello, world!';
var targetSplit = targetString.split('');
function reproduce(parent1, parent2) {
var parent1Split = parent1.split('');
var parent2Split = parent2.split('');
var childSplit = [];
for (var j = 0; j < parent1Split.length; j++) {
var parent1StringCode = parent1Split[j].charCodeAt(0);
var parent2StringCode = parent2Split[j].charCodeAt(0);
//var input = '19.35 19.30 18.88 18.93 18.95 19.03 19.00 18.97 18.97 18.98';
var input = '9.20 8.03 10.02 8.08 8.14 8.10 8.31 8.28 8.35 8.34 8.39 8.45 8.38 8.38 8.32 8.36 8.28 8.28 8.38 8.48 8.49 8.54 8.73 8.72 8.76 8.74 8.87 8.82 8.81 8.82 8.85 8.85 8.86 8.63 8.70 8.68 8.72 8.77 8.69 8.65 8.70 8.98 8.98 8.87 8.71 9.17 9.34 9.28 8.98 9.02 9.16 9.15 9.07 9.14 9.13 9.10 9.16 9.06 9.10 9.15 9.11 8.72 8.86 8.83 8.70 8.69 8.73 8.73 8.67 8.70 8.69 8.81 8.82 8.83 8.91 8.80 8.97 8.86 8.81 8.87 8.82 8.78 8.82 8.77 8.54 8.32 8.33 8.32 8.51 8.53 8.52 8.41 8.55 8.31 8.38 8.34 8.34 8.19 8.17 8.16'
var list = input.split(' ');
var runningBest = [0,0];
for (var i = 0; i < list.length; i++) {
for (var j = i+2; j < list.length; j++) {
if (runningBest[1] - runningBest[0] < list[j]- list[i]) {
runningBest[0] = list[i];
runningBest[1] = list[j]
}
@caleboleary
caleboleary / master_or_variant.html
Created January 13, 2016 19:13
Miva detect whether master or variant, if variant get master info
<mvt:foreach iterator="item" array="order:items">
<mvt:comment>load product from code to get ID</mvt:comment>
<mvt:do name="l.success" file="g.Module_Library_DB" value="Product_Load_Code( l.settings:item:code, l.settings:product )" />
<mvt:comment>load variant information from ID</mvt:comment>
<mvt:do name="l.productvariantpart_count" file="g.Module_Library_DB" value="ProductVariantPartList_Load_Part(l.settings:product:id, l.settings:productvariantparts)" />
<mvt:comment>if any variant parts, get master from this else set master and child accordingly</mvt:comment>
<mvt:if expr="miva_array_elements( l.settings:productvariantparts ) GT 0">
<mvt:do name="l.success" file="g.Module_Library_DB" value="Product_Load_ID( l.settings:productvariantparts[1]:product_id, l.settings:master )" />
<mvt:assign name="l.settings:parentChild:parent" value="l.settings:master:code" />
<mvt:assign name="l.settings:parentChild:child" value="l.settings:item:code" />
@caleboleary
caleboleary / variantarray.html
Last active June 23, 2016 18:54
toolkit variantarray replacement using mvdo
<mvt:comment>
---------------------------------------------START TOOLKIT VARIANTARRAY REPLACEMENT (takes l.settings:product:id)---------------------------------------------
</mvt:comment>
<mvt:comment>
*****Load Attributes*****
</mvt:comment>
<mvt:do file="g.Module_Library_DB" name="l.success" value="AttributeList_Load_Product(l.settings:product:id, l.settings:loaded_attributes)" />
<mvt:if expr="l.settings:loaded_attributes[1]:attemp_id GT 0">
<mvt:assign name="l.templateCode" value="l.settings:loaded_attributes[1]:code" />
@caleboleary
caleboleary / variant_changed.js
Created January 5, 2016 19:05
manually throw miva variant changed attr machine
$('.product-attributes').find('[type="radio"]').on('change', function( event ){
MivaEvents.ThrowEvent( 'variant_changed', { 'product_code': $('#Product_Code').val() , 'variant_id': $(this).prev('label').attr('rel') } );
});