Skip to content

Instantly share code, notes, and snippets.

Avatar
👋
say hi

brad oyler bradoyler

👋
say hi
View GitHub Profile
@bradoyler
bradoyler / declaring.js
Last active Aug 29, 2015
JS console Lesson #1: declaring functions.
View declaring.js
// 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 / array_sorting.js
Last active Aug 29, 2015
JS console lesson : sorting numbers in Array : the functional nature of js
View array_sorting.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 / number_array_manipulation.js
Last active Aug 29, 2015
JS console lesson : manipulating numeric arrays
View number_array_manipulation.js
// 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 / recursive_math.js
Last active Aug 29, 2015
JS console lesson : recursive math
View recursive_math.js
// 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 / find_longest_word.js
Created Feb 22, 2014
JS console challenge : find the longest word
View find_longest_word.js
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 / strings.js
Last active Aug 29, 2015
Lesson #2: string manipulation and counting
View strings.js
//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 / array_filter.js
Last active Aug 29, 2015
JS console lesson : array filtering
View array_filter.js
// 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 / getType.js
Last active Aug 29, 2015
JS console lesson : detect datatype
View getType.js
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])
{
View next_letter.js
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 / inheritance.js
Last active Aug 29, 2015
JS inheritance. Explained.
View inheritance.js
// via http://www.klauskomenda.com/code/javascript-inheritance-by-example/
// create constructor function for base "class"
function Vehicle(hasEngine, hasWheels) {
this.hasEngine = hasEngine || false;
this.hasWheels = hasWheels || false;
}
// create constructor function
function Car (make, model, hp) {
this.hp = hp;