Skip to content

Instantly share code, notes, and snippets.

@krcrawford
Last active March 5, 2019 14:27
Show Gist options
  • Save krcrawford/795a0d1247427be813a05844cd7b5f05 to your computer and use it in GitHub Desktop.
Save krcrawford/795a0d1247427be813a05844cd7b5f05 to your computer and use it in GitHub Desktop.
Javascript Homework #4
/**
* All men are mortal
* Socrates is a man.
* Therefore, socrates is mortal.
*/
const Man = function(name) {
this.name = name;
}
const mortals = [new Man('Socrates'), new Man('Plato'), new Man('Thales')];
const isMortal = (name) => {
let internalName = name;
if (typeof internalName === 'string' && !/[A-Z]/.test.name.charAt(0)) {
internalName = internalName.split('');
internalName[0] = internalName[0].toUpperCase();
internalName = internalName.join('');
}
if (typeof internalName === 'string') {
let i = 0, len = mortals.length;
for (; i < len; i++) {
for (let prop in mortals[i]) {
if (mortals[i].hasOwnProperty('name') && mortals[i] instanceof Man && mortals[i].name === internalName) {
return true;
}
}
}
}
if (typeof internalName === 'undefined' ||
typeof internalName === 'number') {
return false;
}
if (typeof internalName === 'object') {
if (internalName.length) {
return false;
} else if (internalName.hasOwnProperty('name') && typeof internalName.name === 'string') {
let i = 0, len = mortals.length;
for (; i < len; i++) {
for (let prop in mortals[i]) {
if (mortals[i].hasOwnProperty('name') && mortals[i] instanceof Man && mortals[i].name === internalName.name) {
return true;
}
}
}
}
}
return false;
}
/**
* This cake is either vanilla or chocolate.
* This cake is not chocolate.
* Therefore, this cake is vanilla.
*/
const Cake = function(flavor) {
this.flavor = flavor;
}
const flavors = ['chocolate', 'vanilla'];
const makeCakeAndGetFlavor = (flavors, isChocolate) => {
let cake = new Cake(flavors[1]);
if (isChocolate) {
cake = new Cake(flavors[0]);
}
return cake.flavor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment