Advertisement
bebo231312312321

Untitled

Jun 11th, 2023
126
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.     addPlant(plantName, spaceRequired){
  9.         if(this.spaceAvailable < spaceRequired) {
  10.             throw new Error("Not enough space in the garden.")
  11.         }
  12.       //  let plant = {plantName, spaceRequired, ripe: false, quantity:0}
  13.         this.plants.push({plantName, spaceRequired, ripe: false, quantity:0})
  14.         this.spaceAvailable -= spaceRequired //забравил си го
  15.  
  16.         return `The ${plantName} has been successfully planted in the garden.`
  17.     }
  18.  
  19.     ripenPlant(plantName,quantity) {
  20.         let plant = this.plants.find((x) => x.plantName === plantName)
  21.  
  22.         if(plant === undefined) {
  23.             throw new Error (`There is no ${plantName} in the garden.`)
  24.         }
  25.  
  26.         if(plant.ripe === true) {
  27.             throw new Error (`The ${plantName} is already ripe.`)
  28.         }
  29.  
  30.         if (quantity <= 0) {
  31.             throw new Error ("The quantity cannot be zero or negative.")
  32.         }
  33.  
  34.         plant.ripe = true
  35.         plant.quantity += quantity
  36.  
  37.        
  38.         // if(quantity) - бе е необходимo
  39.         if(quantity === 1) {
  40.             return `${quantity} ${plantName} has successfully ripened.`
  41.         } else {
  42.             return `${quantity} ${plantName}s have successfully ripened.` // имаше правописна грешка - не has а have
  43.         }
  44.     }
  45.  
  46.     harvestPlant(plantName) {
  47.         let plant = this.plants.find((x) => x.plantName === plantName)
  48.  
  49.         if(plant === undefined) {
  50.             throw new Error (`There is no ${plantName} in the garden.`)
  51.         }
  52.  
  53.         if(plant.ripe === false) {
  54.             throw new Error (`The ${plantName} cannot be harvested before it is ripe.`)
  55.         }
  56.         this.plants = this.plants.filter(x => x.plantName !== plantName);  // не става с indexOF
  57.         // const index = this.plants.findIndex((x) => x.plantName === plantName);
  58.         //       if (index !== -1) {
  59.         //  this.plants.splice(index, 1);
  60.         //       }
  61.         this.storage.push({plantName, quantity: plant.quantity})
  62.         this.spaceAvailable += plant.spaceRequired
  63.         return `The ${plantName} has been successfully harvested.`
  64.     }
  65.  
  66.    
  67.         generateReport() {
  68.             let result = [`The garden has ${this.spaceAvailable} free space left.`]
  69.        
  70.             let sortedArr = this.plants.sort((a,b) => a.plantName.localeCompare(b.plantName)).map(x => x.plantName).join(", ")  //сортираше storage а трябва да е plants
  71.                
  72.  
  73.                 let plantPrint = `Plants in the garden: ${sortedArr}`
  74.                 result.push(plantPrint);
  75.                
  76.                 let storageString = this.storage // беше го изпуснал
  77.                 .map(x => `${x.plantName} (${x.quantity})`)
  78.                 .join(", ");
  79.                
  80.            let storageArrStttyy = this.storage.length === 0 ? `Plants in storage: The storage is empty.` : `Plants in storage: ${storageString}` //без точка накрая
  81.        
  82.         //    result.push(storageArrStttyy)
  83.         //    return result.join("\n")
  84.         result.push(storageArrStttyy);
  85.         return result.join("\n")
  86.         }
  87.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement