Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Garden {
- constructor(spaceAvailable) {
- this.spaceAvailable = spaceAvailable;
- this.plants = [];
- this.storage = [];
- }
- addPlant(plantName, spaceRequired){
- if(this.spaceAvailable < spaceRequired) {
- throw new Error("Not enough space in the garden.")
- }
- // let plant = {plantName, spaceRequired, ripe: false, quantity:0}
- this.plants.push({plantName, spaceRequired, ripe: false, quantity:0})
- this.spaceAvailable -= spaceRequired //забравил си го
- return `The ${plantName} has been successfully planted in the garden.`
- }
- ripenPlant(plantName,quantity) {
- let plant = this.plants.find((x) => x.plantName === plantName)
- if(plant === undefined) {
- throw new Error (`There is no ${plantName} in the garden.`)
- }
- if(plant.ripe === true) {
- throw new Error (`The ${plantName} is already ripe.`)
- }
- if (quantity <= 0) {
- throw new Error ("The quantity cannot be zero or negative.")
- }
- plant.ripe = true
- plant.quantity += quantity
- // if(quantity) - бе е необходимo
- if(quantity === 1) {
- return `${quantity} ${plantName} has successfully ripened.`
- } else {
- return `${quantity} ${plantName}s have successfully ripened.` // имаше правописна грешка - не has а have
- }
- }
- harvestPlant(plantName) {
- let plant = this.plants.find((x) => x.plantName === plantName)
- if(plant === undefined) {
- throw new Error (`There is no ${plantName} in the garden.`)
- }
- if(plant.ripe === false) {
- throw new Error (`The ${plantName} cannot be harvested before it is ripe.`)
- }
- this.plants = this.plants.filter(x => x.plantName !== plantName); // не става с indexOF
- // const index = this.plants.findIndex((x) => x.plantName === plantName);
- // if (index !== -1) {
- // this.plants.splice(index, 1);
- // }
- this.storage.push({plantName, quantity: plant.quantity})
- this.spaceAvailable += plant.spaceRequired
- return `The ${plantName} has been successfully harvested.`
- }
- generateReport() {
- let result = [`The garden has ${this.spaceAvailable} free space left.`]
- let sortedArr = this.plants.sort((a,b) => a.plantName.localeCompare(b.plantName)).map(x => x.plantName).join(", ") //сортираше storage а трябва да е plants
- let plantPrint = `Plants in the garden: ${sortedArr}`
- result.push(plantPrint);
- let storageString = this.storage // беше го изпуснал
- .map(x => `${x.plantName} (${x.quantity})`)
- .join(", ");
- let storageArrStttyy = this.storage.length === 0 ? `Plants in storage: The storage is empty.` : `Plants in storage: ${storageString}` //без точка накрая
- // result.push(storageArrStttyy)
- // return result.join("\n")
- result.push(storageArrStttyy);
- return result.join("\n")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement