Skip to content

Instantly share code, notes, and snippets.

View rs77's full-sized avatar

Ryan rs77

View GitHub Profile
@rs77
rs77 / collatz.js
Last active August 29, 2015 14:21
// Every number can be reduced to 1 applying the following rules:
// If the number is even divide by two (halve it)
// If the number is odd multiply by 3 and add 1
// Students were asked to find the largest "chain" from numbers 2 to 100.
// Answer is 97 (118) as shown by the following script, then 73 (115), then 54/55 (112).
// Exercise was done as a whole class.
var collatz = function(minNum, maxNum) {
var links = '', temp, steps;
for ( var i = minNum; i <= maxNum; i++ ) {
@rs77
rs77 / removeElementFrom2dArray.js
Last active August 29, 2015 14:21
This function is handy when working within Google Apps Script for Spreadsheets are you are operating with a 2-dimensional array. If you need to pop out an element within an array then this function works best. Need to use the array ".filter" method and call the removeElem callback function passing in the arguments as the second parameter: (In fa…
function removeElem( el ) {
return !~el.indexOf( this.val );
}
// for example:
var arr = [ [ 18, "AAA" ], [ 19, "BBB" ], [ 20, "CCC" ] ];
var query = 19;
console.log( arr.filter( removeElem, { val: query } ) ); // returns [[18, "AAA"],[20,"CCC"]]
@rs77
rs77 / getElementFrom2dArray.js
Last active August 29, 2015 14:21
This function is handy when working with Google App Script for Spreadsheets and you need to get an array out of the 2-dimensional getValues() call. In fact, you may want to look at this performance test and see that running indexOf on array data may be quite slow in comparison to their for-loop and while-loop counterparts as seen here: http://js…
function findElem( el ) {
return !!~el.indexOf( this.val );
}
// for example:
var arr = [ [ 18, "AAA" ], [ 19, "BBB" ], [ 20, "CCC" ] ];
var query = 19;
console.log( arr.filter( findElem, { val: query } ) ); // returns [[19, "BBB"]]
var p = 25000, // principal amount
i = 0.06, // annual interest rate over the life HP
n = 60, // number of periods in life of HP
y = 12, // number of payment periods per annum
t = true, // if payment is to be made in ADVANCE
r = 1 + ( i / y ), // interest rate per period = 1.005
S_n = ( (r^n) - 1 ) ) / ( r - 1 ), // limiting sum of payments = 69.77
repay; // repayments per period
if ( t ) S_n *= r; // = 70.12
if ( cell.length || cell ) // testing if a cell contains something other than an empty string or 0
// parsing through sheet - best practice, grab entire sheet and loop through array
data = sht.getDataRange().getValues();
for ( var r = 0; r < data.length; r += 1 ) { // rows are looped through first
for ( var c = 0; c < data[0].length; c += 1 ) { // columns are next
data[r][c];
}
}
Function AlphaNumericOnly(strSource As String) As String
Dim i As Integer
Dim strResult As String
For i = 1 To Len(strSource)
Select Case Asc(Mid(strSource, i, 1))
Case 32, 48 To 57: 'include 32 if you want to include space
strResult = strResult & Mid(strSource, i, 1)
End Select
Next
@rs77
rs77 / gist:1992537
Created March 7, 2012 11:02
CSS: Image Replacement
.ir {
border:0;
font: 0/0 a;
text-shadow: none;
color: transparent;
background-color: transparent;
}
@rs77
rs77 / html5.html
Created March 7, 2012 10:58
HTML5: Template
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" href="" />
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>