Classes and Constructors Homework
Categories: Javascript HomeworkClasses and Constructors Homework
JavaScript Classes and Constructors Homework
By now you should have a decent grasp of classes and constructors, and how to make and build one. The following exercises should help you solidify your understanding of classes and constructors in JavaScript.
Popcorn Hack 1
- The class TennisPlayer has been defined for you. Create a constructor with the arguements name, rank, and rankPoints.
- Call the class with the arguments Novak Djokovic, 1, 16000.
- Add one or more of the following arguments to the initial constructor: age, tournamentsPlayed, titlesWon. Add this as part of the profile output.
%%javascript
class TennisPlayer {
constructor(name, rank, rankPoints) {
this.name = name;
this.rank = rank;
this.rankPoints = rankPoints;
};
profile () {
console.log("Hi my name is " + this.name + ", my rank is " + this.rank + " and I have " + this.rankPoints + " ranking points.");
}
};
//Call the class here
let myInstance = new TennisPlayer("Johnny", "Novice", 234);
myInstance.profile();
<IPython.core.display.Javascript object>
Popcorn Hack 2
- Create a class called library
- Within library create a class called book with a constructors that allows the two methods - add book and remove book
- Add another inner class called computers - and have it output the number of computers on
Below is the starter code to get you started
%%javascript
// Step 1: Create the main class called Library
class Library {
constructor(name) {
this.name = name;
console.log(`Welcome to the ${this.name} Library!`);
}
// Step 2: Create an inner class called Book
static Book = class {
//create a constructor with that takes the this.books argument.
constructor(books) {
this.books = books;
}
addBook(title) {
this.books.push(title);
console.log(`Added "${title}" to the library.`);
}
removeBook(title) {
const index = this.books.indexOf(title);
if (index > -1) {
this.books.splice(index, 1);
console.log(`Removed "${title}" from the library.`);
} else {
console.log(`"${title}" not found in the library.`);
}
}
}
// Step 3: Create another inner class called Computers
static Computers = class {
// TODO: Create a constructor that takes computersOn as an argument
constructor(ComputersOn) {
this.ComputersOn = ComputersOn;
}
// TODO: Create a method that outputs the number of computers on
showComputersOn() {
console.log(this.ComputersOn);
}
}
}
let myLibrary = new Library("Downtown");
let bookManager = new Library.Book(["How To Walk Your Anish To The Park"]);
let techRoom = new Library.Computers(8);
bookManager.addBook("The Hobbit");
bookManager.addBook("1984");
bookManager.removeBook("The Hobbit");
techRoom.showComputersOn();
<IPython.core.display.Javascript object>
Homework
Create and expand the Cookie Clicker project:
- Fill out the cookies and cookiesPerClick variables.
- Define what should happen upon clicking the cookie.
- Create an
Upgradeclass that multiplies cookies per click, and expand the original cookieclicker class to integrate upgardes. - Print how each upgrade changes the total cookie output.
- Add a cookie type variable which sets the specific type of cookie (ex: Chocolate chip, Oatmeal, etc.) The following code is to help you get started.
Extra credit: Up to 0.03 points
- Create a new cell, apply the ALL of the above changes to a blank cookie clicker project, and submit that code for the cookie clicker project.
%%javascript
class CookieClicker {
constructor() {
this.cookies = 100; //start value -- FREE COOKIES
this.cookiesPerClick = 1;
}
click() {
this.cookies += this.cookiesPerClick;
console.log(`You have ${this.cookies} cookies.`);
}
static Upgrades = class {
constructor(game) {
this.game = game;
}
mimic() {
this.game.cookiesPerClick += 1;
console.log(`Added one more cookie/click, bringing the value to ${this.game.cookiesPerClick} :)`);
}
triplet() {
this.game.cookiesPerClick += 3;
console.log(`Added three more cookies/click, bringing the value to ${this.game.cookiesPerClick} :)`);
}
tentimesbetter() {
this.game.cookiesPerClick += 10;
console.log(`Added ten more cookies/click, bringing the value to ${this.game.cookiesPerClick} :)`);
}
anishthecookies() {
this.game.cookiesPerClick += 10000;
console.log(`Added ten more cookies/click, bringing the value to ${this.game.cookiesPerClick} :)`);
console.log("ANIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIISH!");
}
}
}
let game = new CookieClicker();
game.click();
game.click();
let upgrades = new CookieClicker.Upgrades(game);
upgrades.mimic();
game.click();
game.click();
upgrades.triplet();
game.click();
game.click();
upgrades.tentimesbetter();
game.click();
game.click();
upgrades.anishthecookies();
game.click();
game.click();
<IPython.core.display.Javascript object>
This is where you will find homework: Github Homework Link