Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. var toyota = {
  2. make: "Toyota",
  3. model: "Corolla",
  4. year: 2015,
  5. color: "White",
  6. passengers: 5,
  7. convertible: false,
  8. milage: 58000,
  9. engineStarted: false,
  10. fuelCapacityMax: 19,
  11. fuelLevelCurrent: 0,
  12. fuelAdd: function(fuelToAdd) {
  13.  
  14. var filled = "";
  15.  
  16. if (this.fuelLevelCurrent + fuelToAdd > this.fuelCapacityMax) { // topping off the tank
  17. fuelToAdd = this.fuelCapacityMax - this.fuelLevelCurrent;
  18. filled = "\nTopped off!"
  19. this.fuelLevelCurrent = this.fuelLevelCurrent +
  20. fuelToAdd;
  21. } else if (this.fuelLevelCurrent + fuelToAdd < this.fuelCapacityMax) { // waiting on salary
  22. this.fuelLevelCurrent = this.fuelLevelCurrent +
  23. fuelToAdd;
  24. filled = "\nJust enough..."
  25. }
  26. alert("Added fuel: " + fuelToAdd +
  27. " gal.\n Current fuel: " +
  28. this.fuelLevelCurrent + " gal.\n Max fuel: " +
  29. this.fuelCapacityMax +
  30. " gal." + filled);
  31. },
  32. engineStart: function() {
  33. if (this.fuelLevelCurrent > 0) {
  34. this.engineStarted = true;
  35. } else if (this.fuelLevelCurrent === 0) {
  36. var askFuel =
  37. "Current fuel is zero.\nCurrent capacity is: " +
  38. (this.fuelCapacityMax -
  39. this.fuelLevelCurrent).toString() +
  40. " gal.\nMax capacity is: " + this.fuelCapacityMax
  41. .toString() +
  42. " gal.\nHow much would you like to add?";
  43. this.fuelAdd(parseFloat(prompt(askFuel)));
  44. this.engineStart();
  45. }
  46. },
  47. engineStop: function() {
  48. this.engineStarted = false;
  49. },
  50. drive: function() {
  51. if (this.engineStarted) {
  52. alert("Brooom brooooom!");
  53. } else {
  54. alert("Engine must be started first.");
  55. }
  56. }
  57. };
  58.  
  59. toyota.engineStart();
  60. toyota.drive();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement