Skip to content

Instantly share code, notes, and snippets.

@johnstew
johnstew / Canvas Circle Object
Last active December 12, 2015 08:59
Canvas Circle Object. Looking for advice as to improve this.
window.onload = function(){
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
canvas.width = 500;
canvas.height = 500;
function Circle(posX,posY,r,c,lw,s){
this.x = posX;
this.y = posY;
this.radius = r;
@johnstew
johnstew / jsclass.js
Last active December 12, 2015 09:19
Basic JavaScript Class with gets/sets
window.onload = function(){
function Car(type, year, value){
this.type = type;
this.year = year;
this.value = value;
this.setType = function(t){
this.type = t;
}
@johnstew
johnstew / jsnamespace.js
Created February 10, 2013 17:48
JavaScript Namespace to avoid object/method collisions with other libs.
if(!com_yourname)
com_yourname = {};
else if (typeof (com_yourname) != "object")
throw new Error("com_yourname is not an object");
@johnstew
johnstew / commonobjectmethods.js
Created February 11, 2013 15:01
Common Object Methods
function Rectangle(w,h){
this.width = w;
this.height = h;
}
/* Basic area function.*/
Rectangle.prototype.area = function(){
return this.width * this.height;
}
@johnstew
johnstew / palindrome.js
Created February 14, 2013 19:24
5 minute palindrome tester. breaks for spaces.
function Palindrome(str) {
var temp = str.split("");
var temp2 = [];
for (i = temp.length - 1; i >= 0; i--) {
temp2.push(temp[i]);
}
var strtemp = temp2.join("");
var strtemp2 = temp.join("");
return (strtemp === strtemp2) ? true : false;
}
@johnstew
johnstew / vowelcounter.js
Created February 14, 2013 19:45
Coderbyte challenge. 5 minute vowel counter.
function VowelCount(str) {
var temp = str.split("");
var vowelcount = 0;
function Compare(value) {
var vowels = ["a", "e", "i", "o", "u"];
for (i = 0; i <= vowels.length - 1; i++) {
if (value == vowels[i]) vowelcount++;
}
@johnstew
johnstew / lettercap.js
Created February 14, 2013 20:50
Coderbyte challenge.
function LetterCapitalize(str) {
var temp = str.split(" ");
function Capitalize(value){
var result = value.charAt(0).toUpperCase();
var substring = value.substring(1, value.length);
var final = result+substring;
return final;
}
@johnstew
johnstew / letterchange.js
Created February 17, 2013 23:51
CoderByte Challenge. Letter Change.
function LetterChanges(str) {
var temp = str.split("");
var alpha = "abcdefghijklmnopqrstuvwxyz".split("");
function Change(value) {
var temp2;
if (value == "z") {
return temp2 = "a";
} else if(value.match(/\W/g) || !isNaN(value)){
return temp2 = value;
@johnstew
johnstew / thirdlargest.js
Created February 27, 2013 14:48
CoderByte Challenge: 3rd Largest Word
function ThirdLargest(sArray){
var oArray = {};
for(i = 0; i <= sArray.length-1; i++){
oArray[sArray[i]] = sArray[i].length;
}
var sortable = [];
for(x in oArray){
sortable.push([x, oArray[x]]);
}
@johnstew
johnstew / formatteddivision.js
Created February 27, 2013 15:15
CoderByte Challenge: Formatted Division
function FormattedDivision(num1, num2){
var quotient = num1 / num2;
var fixed = quotient.toFixed(4);
var parts = fixed.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}