Skip to content

Instantly share code, notes, and snippets.

@krcrawford
Last active March 4, 2019 16:42
Show Gist options
  • Save krcrawford/9fcd7d034388f2404f135d823eab7303 to your computer and use it in GitHub Desktop.
Save krcrawford/9fcd7d034388f2404f135d823eab7303 to your computer and use it in GitHub Desktop.
Javascript Homework #3
/**
* All men are mortal
* Socrates is a man.
* Therefore, socrates is mortal.
*/
const Man = function(name) {
this.name = name;
};
// all men are mortals
const isMortal = (t) => t instanceof Man;
// Socrates is a man
const socrates = new Man('Socrates');
/**
* if a man, or men, is a/are mortal(s)...
* and Socrates is a man...
* then, Socrates is a mortal
*/
if (socrates instanceof Man) {
console.log(isMortal(socrates));
}
/**
* This cake is either vanilla or chocolate.
* This cake is not chocolate.
* Therefore, this cake is vanilla.
*/
// This cake is either vanilla or chocolate.
const Cake = function(flavor) {
this.flavors = ['vanilla', 'chocolate'];
if (this.flavors.indexOf(flavor) === -1) {
throw new Error('Flavor not allowed');
};
this.flavor = flavor;
}
const cake = new Cake('vanilla');
// This cake is not chocolate.
console.log(cake.flavor);
if (cake instanceof Cake && cake.flavor !== 'chocolate') {
// Therefore, this cake is vanilla.
console.log(cake.flavor === 'vanilla');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment