Advertisement
cecko

Untitled

Jun 6th, 2023
853
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.     }
  8.  
  9.     addPlant(plantName, spaceRequired) {
  10.         if (this.spaceAvailable - spaceRequired < 0) {
  11.  
  12.             throw new Error('Not enough space in the garden.');
  13.  
  14.         } else {
  15.  
  16.             this.spaceAvailable -= spaceRequired;
  17.  
  18.             let plant = {
  19.                 plantName: plantName,
  20.                 spaceRequired: spaceRequired,
  21.                 ripe: false,
  22.                 quantity: 0
  23.             }
  24.             this.plants.push(plant);
  25.         }
  26.         return this.addPlant;
  27.     }
  28.     ripenPlant(plantName, quantity) {
  29.  
  30.         for (const plant1 of this.plants) {
  31.             plant1.quantity = quantity;
  32.             if (plant1.plantName !== plantName) {
  33.                 throw new Error(`There is no ${plantName} in the garden.`);
  34.             } else if (plant1.ripe === true) {
  35.                 throw new Error(`The ${plantName} is already ripe.`);
  36.             } else if (plant1.quantity <= 0) {
  37.                 throw new Error(`The quantity cannot be zero or negative.`);
  38.             } else {
  39.                 plant1.ripe = true;
  40.                 if (quantity > 0) {
  41.                     console.log(`${quantity} ${plantName} has successfully ripened.`);
  42.                     return `${quantity} ${plantName} has successfully ripened.`;
  43.                 } else {
  44.                     console.log(`${quantity} ${plantName}s have successfully ripened.`);
  45.                     return `${quantity} ${plantName}s have successfully ripened.`
  46.                 }
  47.             }
  48.  
  49.         }
  50.  
  51.  
  52.         return this.ripenPlant;
  53.  
  54.     }
  55. }
  56.  
  57. const myGarden = new Garden(250)
  58. console.log(myGarden.addPlant('apple', 20));
  59. console.log(myGarden.addPlant('orange', 100));
  60. console.log(myGarden.addPlant('cucumber', 30));
  61. console.log(myGarden.ripenPlant('apple', 10));
  62. console.log(myGarden.ripenPlant('orange', 1));
  63. console.log(myGarden.ripenPlant('orange', 4));
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement