Last active
March 5, 2019 14:27
-
-
Save krcrawford/795a0d1247427be813a05844cd7b5f05 to your computer and use it in GitHub Desktop.
Javascript Homework #4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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