Skip to content

Instantly share code, notes, and snippets.

View bradoyler's full-sized avatar
👋
say hi

brad oyler bradoyler

👋
say hi
View GitHub Profile
@bradoyler
bradoyler / array_sorting.js
Last active August 29, 2015 13:56
JS console lesson : sorting numbers in Array : the functional nature of js
// figure out how this works and why it's important
// array
var arr =[2,3,5,5,1,99,22,44];
// sort ascending
arr.sort(function(n1,n2){
return n1 - n2;
}); //output: [1, 2, 3, 5, 22, 44, 99]
@bradoyler
bradoyler / declaring.js
Last active August 29, 2015 13:56
JS console Lesson #1: declaring functions.
// 1. declare a "named" function
function myNamedFunction(){ return true; }
// assertions
console.log(typeof window.myNamedFunction === "function"); // true
console.log(myNamedFunction.name === "myNamedFunction"); // true
// 2. declare anonymous function
var myAnonymousFunction = function(){ return true; };
//assertions
console.log(typeof window.myAnonymousFunction === "function"); // true
@bradoyler
bradoyler / strings.js
Last active August 29, 2015 13:56
Lesson #2: string manipulation and counting
//reverse characters in string
console.log('test string'.split('').reverse().join('')); // output: gnirts tset
// count characters in a string
function Char_Counts(str1) {
var uchars = {};
str1.replace(/\S/g, function(l){
uchars[l] = (isNaN(uchars[l]) ? 1 : uchars[l] + 1);
});
@bradoyler
bradoyler / recursive_math.js
Last active August 29, 2015 13:56
JS console lesson : recursive math
// get factorial of a given number
// ie. 4 = (4 * 3 * 2 * 1) = 24
function factorial(x) {
if(x==0) {
return 1;
}
return x * fact(x-1);
}
@bradoyler
bradoyler / number_array_manipulation.js
Last active August 29, 2015 13:56
JS console lesson : manipulating numeric arrays
// return 2nd highest and lowest numbers
function Second_Greatest_Lowest(arr_num)
{
arr_num.sort(function(x,y)
{
return x-y;
});
var uniqa = [arr_num[0]];
var result = [];
@bradoyler
bradoyler / array_filter.js
Last active August 29, 2015 13:56
JS console lesson : array filtering
// filter words with a RegEx
var startsWithA = new RegExp(/^a/i); // filter by first letter A
var arr = ['Apple','avocado','Banana','Cherry'];
var filtered = arr.filter(function(item){
return startsWithA.test(item);
});
@bradoyler
bradoyler / find_longest_word.js
Created February 22, 2014 22:24
JS console challenge : find the longest word
function LongestWord(sen) {
sen = sen.split(' '); // creates array
var nonAlpha = new RegExp('[^a-zA-Z\d\s:]');
var filtered = sen.filter(function(item){
return !nonAlpha.test(item);
});
var sorted = filtered.sort(function(n1,n2){
@bradoyler
bradoyler / getType.js
Last active August 29, 2015 13:56
JS console lesson : detect datatype
function getType(value)
{
var dtypes = [Function, RegExp, Number, String, Boolean, Object], x, len;
if (typeof value === "object" || typeof value === "function") {
for (x = 0, len = dtypes.length; x < len; x++)
{
if (value instanceof dtypes[x])
{
function LetterChanges(str) {
var letters = str.split('');
for (var i = 0; i < letters.length; i++) {
var letter = letters[i];
if(/[a-yA-Y]/.test(letter)){
letters[i] = String.fromCharCode(letter.charCodeAt(letter.length-1)+1);
}
}
@bradoyler
bradoyler / askwiki.rb
Created February 25, 2014 14:33
a terminal script (Ruby) to query Wikipedia
## Setup steps via terminal:
# 1. create a .rb file : $ touch askwiki.rb
# 2. open file: $ open askwiki.rb
# 3. copy/paste below in file and close/save
# 4. run it: $ ruby askwiki.rb
require 'open-uri'
require 'json'
language = 'en'