Forum Discussion
KSQian
7 years agoContributor
If this is JS you can just work with objects.
a function that takes in a vehicle. You can come up with a spec for the object itself, and and have the function takes in the object and check if the object has certain properties.
Below, a car has 2 properties while a bike only has one. But the function takes care of both cases. It simply check if the vehicle you passed it has an engine...
function upgradeVehicle(vehicle) {
vehicle.speed ++
if (vehicle.engine) {
vehicle.engine === "V8"
}
}
let bicycle = {
speed : 1
}
let car = {
speed : 4,
engine : "oldrustyengine"
}
upgradeVehicle(bicycle) // bicycle.speed == 2
upgradeVehicle(car) // car.speed == 5 ; car.engine == "V8"