JS OOPs Practice Challenges
Index
1. Create a course roster using functions
Create a Student object and a CourseRoster object, and use their prototype settings to create getRoster() and returnGraduatingStudents() object functions. The output of these object functions is described below/
Parameters for the Student Object
name: Stringgrades: An array of integers that rangee from 0-100
Student Object Functions
getGrades: Returns the grades for the studentcheckIsPassing: Returns true if the student is passing (use the providedcalculateGPAfunction)
Parameters for the CourseRoster object
roster: An array ofStudentobjectsteacher: String
CourseRoster object functions
getRoster: Returns a string of student names separated by a commareturnGraduatingStudents: Returns an array of Student objects who will graduate
Result
testCourseRoster: An instance of the CourseRoster object
Example 1
Input:
Result:
2. Create a Book object with functions
We can use the Object.defineProperty() method to define a new property directly on an object. This method takes the object where the property is defined, the property name we want to define, and a descriptor of the property we're defining.
For example, if we have a wrangler object, we can define a new property called maker which sets its maker to "Jeep":
Similarly, Object.create() allows us to create a new object using an already-existing object as the prototype.
Your task: Create a Book object and a ComicBook object. For the Book object, define a setEdition property and a sell() function. For the ComicBook object, use Object.create() to create a relationship between Book and ComicBook.
Parameters for the Book object
title: Stringauthor: Stringquantity: Numberedition: String
Parameters for the ComicBook object
title: Stringauthor: Stringquantity: NumbergraphicArtist: String
Book object functions
setEdition(newEdition): Sets the edition of the booksell(): Sells one copy of the book if there are available copies
Result
[TestBook, TestComicBook]: An array containing an instance of the Book object and an instance of the ComicBook object
Constraints
You must use
definePropertyto declare thesetEditionfunctionYou must use
Object.createto create a relationship betweenBookandComicBook
Example 1 :
Input :
Result :
3. Create a Country with Classes
JavaScript class syntax is a method of creating objects, built on prototypes. Classes use a constructor function which is a special way to initialize objects. Classes can also contain functions that both modify and return data as in this example:
Your task: Create a Country class with two internal methods : getOverview() and setPopulation().
Parameters for the Country class
name: Stringcontinent: Stringcurrency: Stringpopulation: String
Country class functions
getOverview(): Returns an overview of the country's data (for example, "France is a country in Europe. Its currency is the Euro and it has a current population of 67.75 million people.")setPopulation(newPopulation): Sets the population for a country
Result
testCountry: An instance of the Country class
Constraints
You must use JavaScript class syntax
getOverviewmust return a string with the following syntax:
Example 1 :
Input:
Result:
4. Create a Food Ordering Class
JavaSciprt's get syntax allows us to bind a property of an object to a function that will be called when we attempt to liik up its value. This is extremely useful when we want to access a dynamically computed property.
For example, if we have a Person object, and it contains a value called salary, that contains the value of their annual salary in US dollars, but we want to be able to access their salary in Euro, we could use a get function to do compute this conversion as in this example:
The set syntax is similar to get but instead of returning a computed value, it binds the property of an object to a function which is called when setting that property.
For example, if we have an object named device, we can use the set keyword to change the orientation of the device:
Your task: Create an Order class with getters and setters.
Parameters for the Order class
restaurant: Stringtotal: Stringcustomer: String
Properties of the Order Class
The following properties must also be added to the Order class. They are not parameters.
foodStatus = 0;validFoodStatuses = [0,1,2,3];
Order class functions
(get)
orderStatus(): Returns a string that relates to the currentfoodStatus0: "Waiting for the restaurant to accept the order"1: "Your order is being prepared"2: "Your order is ready for pickup"3: "Your order has been cancelled"Error/default: "Something went wrong"
(set)
orderStatus(newStatus): Sets thefoodStatusfor the order. If thenewStatusis invalid, set thefoodStatustonull.
Result
testOrder: An instance of the Order class
Constraints
You must use a getter function for getting the food status
You must use a setter function for setting the food status
Example 1 :
Input:
Result:
5. Create User and Admin Classes
Use private properties to hide certain values and prevent them from being directly accessed. Private properties can only be accessed from inside the class declaration.
We denote a private variable or function with the hash (#) symbol.
For example if we had a Person class with a social security number and a function to calculate their tax bracket, we wouldn't necessarily want that information publicly accessible. So we can make these fields private as in the example below :
Your task: Create a User class and an Admin class. The Admin class extends the User class. The User class has a public updatePassword() function and a private resetPassword() function. The Admin class a public deleteUser() function.
Parameters for the User class
username: String(private) password: String
Parameters for the Admin class
username: String(private) password: String
Properties of the Admin class
The following properties must also be added to the Admin class. They are not parameters.
isAdmin= true;
User class functions
resetPassword(newPassword): Calls the private functionupdatePasswordwith the new passwordupdatePassword(newPassword): Sets the user's password to the newPassword. This is a private function.
Admin class functions
deleteUser(userToDelete): Takes a string,userToDeleteand returns a string message: The user [userToDelete] has been deleted
Result
[testUser, testAdmin]: An array containing an instance of the User class and an instance of the Admin class
Constraints
Password must be a private variable
updatePasswordmust be a private functionYou must use the
extendskeyword to create a relationship betweenUserandAdmin
Example 1:
Input:
Result:
Last updated