The idea of this lab is to get you comfortable with Object Oriented Programming (OOP) and introduce you to Test Driven Development (TDD) in JavaScript. By the end you should be comfortable working with objects and writing prototypes.
All of the tests have been written for you, so all you'll need to do is run them. No need to create any Car objects, because the tests will do that for you.
If any of the tests errors are unclear, take a look at what the test is running within test/carTest.js
- Fork and clone this repository
- Run
yarn install
to install dependencies yarn start
- runmain.js
yarn test
- run test suiteyarn run lint:js
- lint JS
We need a prototype for a car. Can you help us with your sweet JavaScript skills?
But don't worry we'll go step by step.
Under the given file carFn.js
, run all the functions of a Car
object on main.js
.
The requirements to satisfy are listed below:
- Create an object of
Car
that has the following properties:make
model
year
color
seats
previousOwners
- should be initialized to an empty array,
[]
.
- should be initialized to an empty array,
owner
- should be initialized to
manufacturer
.
- should be initialized to
running
- should be initialized to
false
.
- should be initialized to
- Object
Car
must also has these following functions as propertiessell(newOwner)
- We should able to sell a car to someone, which should update the
owner
andpreviousOwners
array. - This takes 1 string parameter for the new owner's name.
- The old owner should be pushed to the end of the
previousOwners
array. - The new
owner
should be set to the parameter passed in.
- We should able to sell a car to someone, which should update the
paint(newColor)
- We should be able to paint the car a new color
- This takes 1 string parameter for the new color's name
- This should update the color of the car to the new color.
Under the given file Car.js
, convert the previous implementation of object
into Class
literal notation. Nothing else should be added to the class
implementation at this point.
Make sure to update the main.js
file accordingly.
Create a test to check whether you've passed the previous requirements. Implement and test the following methods:
feel free to test beyond the suggestions below
Normal cases:
-
var c1 = new Car('Honda', 'Vuzel', 2017, 'red', 7)
- Should expect all properties are set as per initial state
- e.g.
c1.make
=== 'Honda', etc
- e.g.
- Should expect
c1.owner
=== 'manufacturer' - Should expect
c1.running
=== false
- Should expect all properties are set as per initial state
-
c1.sell('prima')
- Should expect
c1.owner
to 'prima' - Should expect
c1.previousOwners
to ['manufacturer']
- Should expect
-
c1.paint('blue')
- Should expect
c1.color
to 'blue'
- Should expect
Abnormal cases:
-
var c2 = new Car()
- Should return === "car can't be instantiated, required parameters are not given"
-
c1.sell(0)
- Should return === 'car can only be sold to a real person with name, please input a string'
-
c1.paint(false)
- Should return === 'car can only be painted with real color, please input a string'
Run all the tests by running yarn test
Continue to write more tests, try to create the assertion first at carTest.js
before writing the implementation code on Car.js
-
Car.start()
- Should change the running value of the car to
true
.
- Should change the running value of the car to
-
Car.off()
- Should change the running value to
false
.
- Should change the running value to
-
Car.driveTo(destination)
- Should
console.log
"driving to <destination>"
, but only if the car is running. - Should return true if it is successful and false if it is not.
- Should
-
Car.park()
- Only if the car is not running, you should console.log
parked!!
. - Should return true if it is successful and false if it is not.
- Only if the car is not running, you should console.log
-
Must have the following constructor parameters:
make
model
year
color
seats
-
By default, a new
Car
should have the following values initialized in the constructor:previousOwners
- should be initialized to an empty array,
[]
.
- should be initialized to an empty array,
owner
- should be initialized to
manufacturer
.
- should be initialized to
running
- should be initialized to
false
.
- should be initialized to
-
We should be able to do the following with our car:
Car.sell(newOwner)
- We should able to sell a car to someone, which should update the
owner
andpreviousOwners
array. - This takes 1 string parameter for the new owner's name.
- The old owner should be pushed to the end of the
previousOwners
array. - The new
owner
should be set to the parameter passed in.
- We should able to sell a car to someone, which should update the
Car.paint(newColor)
- We should be able to paint the car a new color
- This takes 1 string parameter for the new color's name
- This should update the color of the car to the new color.
Implement and test the following methods:
Car.start()
- Should change the running value of the car to
true
.
- Should change the running value of the car to
Car.off()
- Should change the running value to
false
.
- Should change the running value to
Car.driveTo(destination)
- Should
console.log
"driving to <destination>"
, but only if the car is running. - Should return true if it is successful and false if it is not.
- Should
Car.park()
- Only if the car is not running, you should console.log
parked!!
. - Should return true if it is successful and false if it is not.
- Only if the car is not running, you should console.log
Add the following property as a parameter to the constructor:
passengers
- Should be optional and default to an empty array if not specified.
Implement the following methods:
Car.pickUp(name)
- Should take a
name
andconsole.log
that you are"driving to pick up <name>"
, but only if thecar
is running AND there are enough seats available. - Should also update the
passengers
array to include the new passenger. - Should also return true on success and false on failure.
- The newly picked up passenger should be
pushed
to the end of the array.
- Should take a
Car.dropOff(name)
- Should take a
name
and remove them from thepassengers
array, but only if they are in the array. - Should also only drop them off if the car is
on
. - Should also output
"driving to drop off <name>"
and return true on success and false on failure.
- Should take a
Car.passengerCount()
- Should return the number (integer) of passengers currently in the car.
NOTE: When deciding if there are enough seats available, remember that the driver takes up 1 seat, but is NOT counted as a passenger in passengerCount(). You can assume the driver is the owner.
- All content is licensed under a CC-BY-NC-SA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact legal@ga.co.