Classes and Constructors Homework
Classes 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) {
// Add code here
};
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
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
// 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.
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
// TODO: Create a method that outputs the number of computers on
}
}
// --- Example Usage ---
// Uncomment after writing your constructors!
// const myLibrary = new Library("Downtown");
// const bookManager = new Library.Book();
// const techRoom = new Library.Computers(8);
// bookManager.addBook("The Hobbit");
// bookManager.addBook("1984");
// bookManager.removeBook("The Hobbit");
// techRoom.showComputersOn();
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(cookies,cookiesPerClick) {
this.cookies = ____; //start value
this.cookiesPerClick = ____;
}
click() {
this.cookies += this.cookiesPerClick;
console.log(`You have ${this.cookies} cookies.`);
}
}
This is where you will find homework: Github Homework Link