Skip to content

Instantly share code, notes, and snippets.

View gmathew2014's full-sized avatar

George gmathew2014

  • Sprout Social
  • Chicago, IL
View GitHub Profile
@gmathew2014
gmathew2014 / gist:adf6e8fc832b6a3c2ae3
Created September 29, 2014 03:36
Reverse a string (using split, reverse, join methods)
//Coderbyte challenge
var FirstReverse = function(str) {
return str.split('').reverse().join('');
}
FirstReverse('Test'); // -> tseT
// Calculate the last prime number under 1000
var isPrime = function(num) {
if (num < 2) {
return false;
}
for (var i = 2; i < num; i++) {
if (num % i === 0) {
return false;
}
// Codeval problem to solve the sum of the first 1000 primes
var isPrime = function(num) {
if (num < 2) {
return false;
}
for (var i = 2; i < num; i++) {
if (num % i === 0) {
return false;
}
// Simple function for converting string to lowercase
var textConvert = function(a){
if (typeof a == 'string') {
$('body').append(a.toLowerCase() + '</br>');
}
else {http://jsfiddle.net/user/dashboard/
$('body').append('Please input a string');
}
};
// Duplicate a numeric array
var numArray = [1, 2, 3, 4, 5];
var duplicate = function(a) {
for (var i = 0; i < 1; i++) {
a = a + a;
}
return a;
};
// A simple modulus function
var mod = function(a,b) {
var c = Math.floor(a / b);
var d = b * c;
var e = a - d;
return e;
};
$('body').append(mod(25,5));
@gmathew2014
gmathew2014 / Alphabet concatenation
Created September 28, 2014 21:35
Alphabet concatenation
// Program to take an array of the alphabet and pair every letter of the alphabet
// with every other letter of the alphabet
// First, we define the array
var xyz = ['A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
// Output the solution with two loops
// isEven() recursion function to test if numbers are even
// with the following rules
// if n == 0, then it is even
// if n == 1, then it is odd, and false
// if n < 0, or negative, then we have a recursion that negates the number and restarts the function
// otherwise, all other numbers will recursively test if they are even by subtracting -2
// until the program goes to the first two conditions
// For really big numbers the stack overflows and the simpler n % 2 == 0 condition is more efficient
function isEven(number) {
@gmathew2014
gmathew2014 / Minimum function
Created August 21, 2014 06:21
Eloquent JavaScript, Chapter 3, Minimum function
// My minimum function alternative to Math.min()
function min(a,b) {
if (a < b)
return a;
else
return b;
}
console.log(min(0, 10));
@gmathew2014
gmathew2014 / Chessboard
Last active August 29, 2015 14:05
Chapter 2, Eloquent JavaScript. "Chessboard"
// Chessboard exercise
var size = 8;
var chess = "";
for (y = 0; y < size; y++) {
for (x = 0; x < size; x++) {
if ((x+y) % 2 == 0)
chess += "#";
else
chess += " ";