Skip to content

Instantly share code, notes, and snippets.

View macikokoro's full-sized avatar

Joaquin Guardado macikokoro

View GitHub Profile
@macikokoro
macikokoro / even.js
Last active August 29, 2015 14:04
Basic conditional
var num = 10;
while(num > 0) {
if(num % 2 === 0) {
console.log(num);
}
num--;
}
function oldestLivingParent(people) {
var livingParents = {};
var sortedByAge = [];
var oldestLivingParent = false;
var isDead = function(p) {
return !person.age;
};
var isParent = function(p) {
return !!livingParent[p.name];
};
@macikokoro
macikokoro / gpa.js
Last active August 29, 2015 14:04
Use of public properties.
function StudentReport() {
this.grade1 = 4;
this.grade2 = 2;
this.grade3 = 1;
// to hide the grades and to display gpa only
// change this to var in grade properties.
this.getGPA = function() {
return (this.grade1 + this.grade2 + this.grade3) / 3;
};
}
@macikokoro
macikokoro / mrFluffy.js
Created July 21, 2014 06:29
Prototype again, this time teaching dogs to say hello.
function Dog (breed) {
this.breed = breed;
};
// sayHello method for the Dog class
// all dogs can now say hello
Dog.prototype.sayHello = function() {
console.log("Hello I'm a " + this.breed + " dog.");
}
@macikokoro
macikokoro / typeof.js
Last active August 29, 2015 14:04
Using a typeof to print only strings.
var languages = {
english: "Hello!",
french: "Bonjour!",
notALanguage: 4,
spanish: "Hola!"
};
// prints hello in 3 different languages.
// for in loops through all the properties one by one
// assigning the property name to x on each run of the loop.
@macikokoro
macikokoro / argument.js
Last active August 29, 2015 14:04
Example of passing an argument.
function Person(first,last,age) {
this.firstname = first;
this.lastname = last;
this.age = age;
var bankBalance = 7500;
this.askTeller = function(pass) {
if (pass == 1234) return bankBalance;
else return "Wrong password.";
};
@macikokoro
macikokoro / pMethod.js
Created July 21, 2014 05:09
Accessing private methods
function Person(first,last,age) {
this.firstname = first;
this.lastname = last;
this.age = age;
var bankBalance = 7500;
var returnBalance = function() {
return bankBalance;
};
@macikokoro
macikokoro / private.js
Created July 21, 2014 04:50
Accessing private variables in objects.
function Person(first,last,age) {
this.firstname = first;
this.lastname = last;
this.age = age;
var bankBalance = 7500;
this.getBalance = function() {
// return the bankBalance
return bankBalance;
};
@macikokoro
macikokoro / theChain.js
Created July 21, 2014 03:26
Prototype chaining example.
// original classes
function Animal(name, numLegs) {
this.name = name;
this.numLegs = numLegs;
this.isAlive = true;
}
function Penguin(name) {
this.name = name;
this.numLegs = 2;
}
@macikokoro
macikokoro / emperor.js
Created July 21, 2014 02:13
Another class with inheritance.
function Penguin(name) {
this.name = name;
this.numLegs = 2;
}
// the Emperor class
function Emperor(name) {
this.name = name;
}