Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var toyota = {
- make: "Toyota",
- model: "Corolla",
- year: 2015,
- color: "White",
- passengers: 5,
- convertible: false,
- milage: 58000,
- engineStarted: false,
- fuelCapacityMax: 19,
- fuelLevelCurrent: 0,
- fuelAdd: function(fuelToAdd) {
- var filled = "";
- if (this.fuelLevelCurrent + fuelToAdd > this.fuelCapacityMax) { // topping off the tank
- fuelToAdd = this.fuelCapacityMax - this.fuelLevelCurrent;
- filled = "\nTopped off!"
- this.fuelLevelCurrent = this.fuelLevelCurrent +
- fuelToAdd;
- } else if (this.fuelLevelCurrent + fuelToAdd < this.fuelCapacityMax) { // waiting on salary
- this.fuelLevelCurrent = this.fuelLevelCurrent +
- fuelToAdd;
- filled = "\nJust enough..."
- }
- alert("Added fuel: " + fuelToAdd +
- " gal.\n Current fuel: " +
- this.fuelLevelCurrent + " gal.\n Max fuel: " +
- this.fuelCapacityMax +
- " gal." + filled);
- },
- engineStart: function() {
- if (this.fuelLevelCurrent > 0) {
- this.engineStarted = true;
- } else if (this.fuelLevelCurrent === 0) {
- var askFuel =
- "Current fuel is zero.\nCurrent capacity is: " +
- (this.fuelCapacityMax -
- this.fuelLevelCurrent).toString() +
- " gal.\nMax capacity is: " + this.fuelCapacityMax
- .toString() +
- " gal.\nHow much would you like to add?";
- this.fuelAdd(parseFloat(prompt(askFuel)));
- this.engineStart();
- }
- },
- engineStop: function() {
- this.engineStarted = false;
- },
- drive: function() {
- if (this.engineStarted) {
- alert("Brooom brooooom!");
- } else {
- alert("Engine must be started first.");
- }
- }
- };
- toyota.engineStart();
- toyota.drive();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement