Advertisement
cecko

Untitled

Jun 6th, 2023
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Garden {
  2.     constructor(spaceAvailable) {
  3.         this.spaceAvailable = spaceAvailable;
  4.         this.plants = [];
  5.         this.storage = [];
  6.     }
  7.     addPlant(plantName, spaceRequired) {
  8.         if (this.spaceAvailable - spaceRequired < 0) {
  9.  
  10.             throw new Error('Not enough space in the garden.');
  11.  
  12.         } else {
  13.  
  14.             this.spaceAvailable -= spaceRequired;
  15.  
  16.             let plant = {
  17.                 plantName: plantName,
  18.                 spaceRequired: spaceRequired,
  19.                 ripe: false,
  20.                 quantity: 0
  21.             }
  22.             this.plants.push(plant);
  23.         }
  24.         return this.addPlant;
  25.     }
  26.     ripenPlant(plantName, quantity) {
  27.  
  28.         if (plant.plantName !== plantName) {
  29.             throw new Error(`There is no ${plantName} in the garden.`);
  30.         }
  31.         if (plant.ripe === true) {
  32.             throw new Error(`The ${plantName} is already ripe.`);
  33.         }
  34.         if (plant.quantity <= 0) {
  35.             throw new Error(`The quantity cannot be zero or negative.`);
  36.         }
  37.  
  38.         return this.ripenPlant;
  39.  
  40.     }
  41. }
  42.  
  43. const myGarden = new Garden(250)
  44. console.log(myGarden.addPlant('apple', 20));
  45. console.log(myGarden.addPlant('orange', 100));
  46. console.log(myGarden.addPlant('cucumber', 30));
  47. console.log(myGarden.ripenPlant('apple', 10));
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement