Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions classes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class Fruit {
constructor(options) {
this.type = options.type;
this.name = options.name;
this.isRipe = options.isRipe;
this.calories = options.calories;
}

ship(destination) {
console.log(`shipping ${this.name} to ${destination}`);
}
}

// raspberry -----------

class Raspberry extends Fruit { // connects prototype
constructor(options) {
super(options); // Fruit.call
this.doBearsLikeIt = options.doBearsLikeIt;
}

foo() {
console.log('foo!');
}
}

// pineapple -----------

class Pineapple extends Fruit {
constructor(options) {
super(options);
this.fromHawaii = options.fromHawaii;
}
}

// instantiation -----------
const myRaspberry = new Raspberry({
type: 'berry',
name: 'raspberry',
isRipe: true,
calories: 200,
doBearsLikeIt: true,
});

const myPineapple = new Pineapple({
type: 'bush thing',
name: 'pineapple',
isRipe: true,
calories: 1500,
fromHawaii: true,
});

myRaspberry.ship('Alaska');
myPineapple.ship('Arkansas');
myRaspberry.foo();
45 changes: 45 additions & 0 deletions constructors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// to test these problems you can run 'node constructors.js' in your terminal

// problem #1
// add a method to Animal's prototype called 'grow'
// when 'grow' is invoked log '<name> grew larger!'

function Animal(options) {
this.name = options.name;
this.grow = function () {
return this.name + ' grew larger!';
};
}
// add 'grow' to Animal's prototype here

// problem #2
// setup Cat to inherit from Animal
// the Animal constructor needs to be invoked with the 'options' argument
// Cat should have its prototype inherit from Animal
// instances of Cat should also have access to the 'grow' method

function Cat(options) {
// invoke Animal here with .call
class Cat extends Animal {
constructor(options) {
super(options);
// invoke Animal here with .call
};


const foofie = new Cat({
name: 'foofie',
});
foofie.grow();
}

// connect the prototypes here

// if everything is setup properly the code below will print 'Foofie grew larger!'
// uncomment the code below to test your solution

const foofie = new Cat({
name: 'foofie',
});

foofie.grow();
29 changes: 9 additions & 20 deletions recursion.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// to test these problems you can run 'node recursion.js' in your terminal
// Problem 1:
/ Problem 1:

let n = 1;
while (n <= 10) {
Expand All @@ -9,25 +8,15 @@ while (n <= 10) {

// write a recursive - function called countToTen that mimics the while loop above.

// code here
for (i=0; i<=10; i++){
setTimeout(function(){
$(".test").html(i);
}?
},10000);

// when you code is ready, un-comment the next line and run the file
// console.log(countToTen());
/* ================ Next Problem ================= */

// Problem 2:
console.log(counttoTen());

const factorial = n => {
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
};

console.log(factorial(5));

// write the above functionin a recursive way.

// when you code is ready, un-comment the next line and run the file
// console.log(recursiveFactorial());
// console.log(countToTen());
/* ================ Next Problem ================= */