diff --git a/spec/test.js b/spec/test.js index de4c18b..eba5b48 100644 --- a/spec/test.js +++ b/spec/test.js @@ -18,70 +18,70 @@ describe('Car', function(){ }); describe('#state', function(){ - xit('should initially be off', function(){ + it('should initially be off', function(){ expect(myCar.state).to.equal("off"); }); }); describe('#previousOwners', function(){ - xit('should exist and initially be empty', function(){ + it('should exist and initially be empty', function(){ expect(myCar.previousOwners).to.exist.to.be.empty; }); }); describe('#currentOwner', function(){ - xit('should initially be manufacturer', function(){ + it('should initially be manufacturer', function(){ expect(myCar.currentOwner).to.equal("Manufacturer"); }); }); describe('#passengers', function(){ - xit('should exist and initially be empty', function(){ + it('should exist and initially be empty', function(){ expect(myCar.passengers).to.exist.to.be.empty; }); }); describe('#sale', function(){ - xit('should move currentOwner to previousOwners array', function(){ + it('should move currentOwner to previousOwners array', function(){ myCar.sale("Charlie"); expect(myCar.previousOwners[0]).to.equal("Manufacturer"); }); - xit('should update currentOwner with the new owner', function(){ + it('should update currentOwner with the new owner', function(){ myCar.sale("Charlie"); expect(myCar.currentOwner).to.equal("Charlie"); }); }); describe('#paint', function(){ - xit('should update the color of myCar', function(){ + it('should update the color of myCar', function(){ myCar.paint("Blue"); expect(myCar.color).to.equal("Blue"); }); }); describe('#start', function(){ - xit('should update the state to on', function(){ + it('should update the state to on', function(){ myCar.start(); expect(myCar.state).to.equal("on"); }); }); describe('#off', function(){ - xit('should update the state to off', function(){ + it('should update the state to off', function(){ myCar.off(); expect(myCar.state).to.equal("off"); }); }); describe('#pickUp', function(){ - xit('should add the passenger to the passengers array', function(){ + it('should add the passenger to the passengers array', function(){ myCar.start(); myCar.pickUp("john"); expect(myCar.passengers[0]).to.equal("john"); }); - xit('should only modify passengers array if car is on', function(){ + it('should only modify passengers array if car is on', function(){ // myCar.off(); myCar.pickUp("john"); expect(myCar.passengers[0]).to.be.empty; @@ -89,14 +89,14 @@ describe('Car', function(){ }); describe('#dropOff', function(){ - xit('should remove passenger from the passengers array', function(){ + it('should remove passenger from the passengers array', function(){ myCar.start(); myCar.pickUp("john"); myCar.dropOff("john"); expect(myCar.passengers).to.be.empty; }); - xit('should leave passenger in the passengers array if car is off', function(){ + it('should leave passenger in the passengers array if car is off', function(){ myCar.start(); myCar.pickUp("john"); myCar.off(); diff --git a/src/car.js b/src/car.js index 5a72227..2b9e0fc 100644 --- a/src/car.js +++ b/src/car.js @@ -1,14 +1,50 @@ function Car(make, model, year, color){ - + this.make = make; + this.model = model; + this.year = year; + this.color = color; + this.state = 'off'; + this.previousOwners = []; + this.currentOwner = 'Manufacturer'; + this.passengers = []; } Car.prototype.sale = function(newOwner){ - + this.previousOwners.push(this.currentOwner); + this.currentOwner = newOwner; }; Car.prototype.paint = function(newColor){ + this.color = newColor; +}; + +Car.prototype.start = function() { + this.state = 'on'; +}; +Car.prototype.off = function() { + this.state = 'off'; }; +Car.prototype.pickUp = function(passenger) { + if (this.state === "on") { + this.passengers.push(passenger); + } +}; + +Car.prototype.dropOff = function(passenger) { + if (this.state === 'on') { + for (i = 0; i < this.passengers.length; i++) { + if (this.passengers[i] === passenger) { + this.passengers.splice(i, 1); + } + } +// Alternative for going through an array when the array item is known: +// +// var passengerIndex = this.passengers.indexOf(passengers); +// this.passengers.splice(passengerIndex, 1); + + } +} module.exports=Car; \ No newline at end of file