Skip to content

Instantly share code, notes, and snippets.

@rd13
rd13 / bestsum.js
Created May 25, 2012 09:43
Find the best sum of numbers in an array for a given value.
//jsFiddle: http://jsfiddle.net/CuDzh/3/
$(function() {
function bestSum(numbers, sum) {
l = function(index, total, solution) {
index = index ? index : 0;
total = total ? total : 0;
solution = solution ? solution : '';
@rd13
rd13 / gist:2888660
Created June 7, 2012 12:52
Javascript Generate Password
generatePassword = function(l) {
l = l ? l : 8;
var charset = "abcdefghijklnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
retVal = "";
for (var i = 0, n = charset.length; i < l; ++i) {
retVal += charset.charAt(Math.floor(Math.random() * n));
}
return retVal;
}
@rd13
rd13 / gist:3136066
Created July 18, 2012 13:00
jQuery multiple deferreds
/* http://jsfiddle.net/Zp3MZ/1/ */
$(function() {
var objs = [{'one':1},{'two':2},{'three':3}];
ajaxCall = function(i) {
return $.ajax({
url:'/echo/html/',
method: 'POST',
@rd13
rd13 / gist:3228477
Created August 1, 2012 16:29
TR Inset Shadow CSS
/* http://jsfiddle.net/KtPdt/8/ */
td {
background-color: rgba(255, 255, 255, 0.8);
-webkit-box-shadow: inset 0px 11px 8px -10px orange,
inset 0px -11px 8px -10px orange;
padding: 5px;
}
td:first-child {
-webkit-box-shadow: inset 10px 11px 8px -10px orange,
@rd13
rd13 / gist:3793748
Created September 27, 2012 12:31
Mongoose / MongoDB Data Types
mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
Schema = mongoose.Schema;
console.log(Schema.Types);
{ String: [Function: SchemaString],
Number: [Function: SchemaNumber],
Boolean: { [Function: SchemaBoolean] '$conditionalHandlers': { '$in': [Function: handleArray] } },
DocumentArray: [Function: DocumentArray],
@rd13
rd13 / gist:3924792
Created October 20, 2012 21:01
PHP number_format in Javascript
//UK format - commas for thousands separator, full stop for decimal.
//d = number of decimal points.
String.prototype.number_format = function(d) {
var n = this;
var c = isNaN(d = Math.abs(d)) ? 2 : d;
var s = n < 0 ? "-" : "";
var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + ',' : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + ',') + (c ? '.' + Math.abs(n - i).toFixed(c).slice(2) : "");
}
@rd13
rd13 / gist:4038271
Created November 8, 2012 11:29
Disqus / Handlebars Block Helper. (Node / Express hbs)
//A handlebars block helper for embedding Disqus.
var hbs = require('hbs');
hbs.registerHelper('disqus', function(slug) {
var value = "<div id='disqus_thread'></div>"+
"<script type='text/javascript'>"+
"var disqus_shortname = '';"+
"var disqus_identifier = '/"+slug+"';"+
"(function() {"+
@rd13
rd13 / gist:4079164
Created November 15, 2012 15:25
in_array function in Javascript
Array.prototype.contains = function(obj) {
var i = this.length;
while (i--) {
if (this[i] === obj) {
return true;
}
}
return false;
}
@rd13
rd13 / gist:4454684
Created January 4, 2013 18:18
Objective-C UICollectionViewCell Iteration
//cv = UICollectionView
for (int section = 0; section < [cv numberOfSections]; section++) {
for (int row = 0; row < [cv numberOfItemsInSection:section]; row++) {
NSIndexPath* cellPath = [NSIndexPath indexPathForRow:row inSection:section];
//Cast to a custom cell: "GameCollectionViewCell"
GameCollectionViewCell *cell = (GameCollectionViewCell *) [cv cellForItemAtIndexPath:cellPath];
NSLog(@"%i",cell.lbl.tag);
}
}
@rd13
rd13 / gist:4454720
Created January 4, 2013 18:22
Simple Objective-C Timer Example
//Do something 2 seconds after view loads
- (void)viewDidLoad
{
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(theAction)
userInfo:nil