Tengan en cuenta algo que no dice el ejercicio por lo cual me demoré yo tambien más de la cuenta.
En la clase Cart deben almacenar los productos en la propiedad products o no podrán pasar por todas las pruebas ya que hay dos que verifican la longitud de esta propiedad.
Literal me quede como 10 minutos viendo porque estaba mal mi código, bueno abajo les dejo mi solución.
Solución:
.
.
.
.
.
.
.
import{Product}from"./product";exportclassArticleextendsProduct{constructor(name, price, quantity){super(name, price, quantity)}addToCart(){return`Agregando ${this.quantity} unidades del artículo ${this.name} al carrito`}}exportclassServiceextendsProduct{constructor(name, price, quantity){super(name, price, quantity)}addToCart(){return`Agregando el servicio ${this.name} al carrito`}}exportclassCart{constructor(products =[]){this.products= products
}addProduct(product){ product.addToCart()this.products.push(product)}deleteProduct(product){let index =this.products.findIndex(item=> item.name=== product.name)this.products.splice(index,1);}calculateTotal(){let total
returnthis.products.reduce((total, item)=>{ total += item.price* item.quantityreturn total
},0)}}
Hola, ten presente que los constructores en las clases que heredan sólo son necesarios si se agregan atributos propios de la clase, de lo contrario sobran.
En la clase Cart tampoco sería necesario el constructor a menos que se vaya a inicializar los productos, bastaría sólo con colocar la variable como: products = []; al inicio del cuerpo de la clase Cart.
Me llevo este feedback para agregarlo a la guía 💪
import{Product}from"./product";exportclassArticleextendsProduct{// Tu código aquí 👈addToCart(){return`Agregando ${this.quantity} unidades del artículo ${this.name} al carrito`;}}exportclassServiceextendsProduct{// Tu código aquí 👈addToCart(){return`Agregando servicio ${this.name} al carrito`;}}exportclassCart{// Tu código aquí 👈 products =[];addProduct(product){this.products.push(product);return product.addToCart();}deleteProduct(product){this.products=this.products.filter((p)=> p.name!== product.name); product.addToCart();}calculateTotal(){returnthis.products.reduce((sum, curr)=> sum + curr.price* curr.quantity,0);}getProducts(){returnthis.products;}}
Hola, les comparto mi solucion
Como nota, la variable para almacenar debe de ser products sino las pruebas no pasaran, creo que hace falta eso en la guia 😅
import{Product}from"./product";exportclassArticleextendsProduct{// Tu código aquí 👈constructor(name, price, quantity){super(name, price, quantity)}addToCart(){return`Agregando ${this.quantity} unidades del artículo ${this.name} al carrito"`}}exportclassServiceextendsProduct{// Tu código aquí 👈constructor(name, price, quantity){super(name, price, quantity)}addToCart(){return`Agregando el servicio ${this.name} al carrito`}}exportclassCart{// Tu código aquí 👈constructor(){this.products=[];}addProduct(product){ product.addToCart()this.products.push(product)}deleteProduct(product){const index =this.products.findIndex((item)=> item.name=== product.name&& item.price=== product.price&& item.quantity=== product.quantity)this.products.splice(index,1)}calculateTotal(){returnthis.products.reduce((acumulador, numero)=>(acumulador +(numero.price* numero.quantity)),0);}}
My solution
58
58
58
import{Product}from"./product";exportclassArticleextendsProduct{addToCart(){return`Agregando ${this.quantity} unidades del artículo ${this.name} al carrito`}}exportclassServiceextendsProduct{addToCart(){return`Agregando el servicio ${this.name} al carrito`}}exportclassCart{constructor(){this.products=[]}addProduct(product){this.products.push(product)return product.addToCart()}deleteProduct(product){const idx =this.products.findIndex(({ name })=> product.name=== name)if(idx >-1){this.products.splice(idx,1)}}calculateTotal(){returnthis.products.reduce((sum, prod)=> sum + prod.quantity* prod.price,0)}getProducts(){returnthis.products}}
Mi solución
import{Product}from"./product";exportclassArticleextendsProduct{addToCart(){return`Agregando ${this.quantity} unidades del artículo ${this.name} al carrito`;}}exportclassServiceextendsProduct{addToCart(){return`Agregando el servicio ${this.name} al carrito`;}}exportclassCart{constructor(){this.products=[];}addProduct(product){this.products.push(product);return product.addToCart();}deleteProduct(product){let itemIdx =this.products.findIndex((item)=>{return item.name=== product.name;});if(itemIdx >-1){returnthis.products.splice(itemIdx,1);}}calculateTotal(){returnthis.products.reduce((acum, product)=> acum + product.price* product.quantity,0);}getProducts(){returnthis.products;}}
He disfrutado mucho el curso, pero al mismo tiempo no niego que no deja de ser un dolor de cabeza cuando se llega a los retos. Siempre antes de llenar el ejercicio, ocupo en desarrollar mi código en mi editor de código, una vez estando seguro entonces vengo a llenar mi reto, con la gran sorpresa de que acá nunca me funciona a la primera. En este caso me di cuenta que no redactaron algo que era necesario para completar el reto.
Sugerencia: Por favor una mejor redacción nos ayudaría a todos a ahorrar algo de tiempo, de verdad.
No me corrio en playground no se que tengo mal
exportclassArticleextendsProduct{addToCart(){super.addToCart();return`Agregando ${this.quantity} unidades del artículo ${this.name} al carrito`;}}exportclassServiceextendsProduct{addToCart(){super.addToCart();return`Agregando el servicio ${this.name} al carrito`;}}exportclassCart{constructor(){this.products=[];}addProduct(product){this.products.push(product);return product.addToCart();}deleteProduct(product){const index =this.products.findIndex(element=> element.name=== product.name);this.products.splice(index,1);}calculateTotal(){returnthis.products.reduce((a, c)=> a + c.quantity* c.price,0);}getProducts(){returnthis.products;}}
Hola. Quita el return del metodo addProduct() y quita las llamadas a super en las clases Article y Service. Ya con esos cambios pasas la prueba. Espero te ayude.
Mi solución 👇
import{Product}from"./product";classArticleextendsProduct{addToCart(){return`Agregando ${this.quantity} unidades del artículo ${this.name} al carrito`;}}classServiceextendsProduct{addToCart(){return`Agregando el servicio ${this.name} al carrito`;}}classCart{ products =[];addProduct(product){this.products.push(product);return product.addToCart();}deleteProduct(product){const index =this.products.findIndex((p)=> p.name=== product.name);this.products.splice(index,1);}calculateTotal(){const total =this.products.reduce((acc, val)=>{const price = val.price* val.quantity;return price + acc;},0);return total;}getProducts(){returnthis.products;}}
Desafio superado!
Hay que destacar que en el texto no se especifica que se debe crear un método de los productos en la clase cart.
Aquí está mi solución:
exportclassArticleextendsProduct{addToCart(){return`Agregando ${this.quantity} unidades del artículo ${this.name} al carrito`}}exportclassServiceextendsProduct{addToCart(){return`Agregando el servicio ${this.name} al carrito`;}}exportclassCart{constructor(){this.products=newArray;}addProduct(product){this.products.push(product) product.addToCart();}deleteProduct(product){this.products.splice(this.products.findIndex(products=> products.name== product.name),1)}calculateTotal(){returnthis.products.map(element=> element.price*element.quantity).reduce((sum, element)=> sum + element,0)}getProducts(){returnthis.products}}
🛡️🛡️🛡️Escudo anti spoilers🛡️🛡️🛡️
Sistema de carrito de compras
!Spoiler Shield
exportclassArticleextendsProduct{// Tu código aquí 👈constructor(name, price, quantity){super(name, price, quantity);}addToCart(){return`Agregando ${this.quantity} unidades del articulo ${this.name} al carrito`;}}exportclassServiceextendsProduct{constructor(name, price, quantity){super(name, price, quantity);}addToCart(){return`Agregando el servicio ${this.name} al carrito`;}// Tu código aquí 👈}exportclassCart{constructor(){this.products=[];}// Tu código aquí 👈addProduct(product){ product.addToCart();this.products.push(product);}deleteProduct(product){this.products=this.products.filter((cartItem)=> cartItem.name!= product.name);}calculateTotal(){let total =0;for(let item ofthis.products){ total += item.price* item.quantity;}return total;}}
exportclassArticleextendsProduct{constructor(name, price, quantity){super(name, price, quantity)}addToCart(){return`Agregando ${this.quantity} unidades del artículo ${this.name} al carrito`}}exportclassServiceextendsProduct{constructor(name, price, quantity){super(name, price, quantity)}addToCart(){return`Agregando el servicio ${this.name} al carrito`}}exportclassCart{constructor(){this.cartList=[]}addProduct(product){ product.addToCart()this.cartList.push({name: product.name,price: product.price,quantity: product.quantity})}deleteProduct(product){const index =this.cartList.findIndex(obj=>JSON.stringify(obj)===JSON.stringify(product))this.cartList.splice(index,1)}calculateTotal(){const total =this.cartList.reduce((sum, item)=> sum + item.price* item.quantity,0)return total
}getProducts(){returnthis.cartList}}
Vamoooss!!!!
.
.
.
.
.
.
.
.
..
.
.
.
import{Product}from"./product";exportclassArticleextendsProduct{addToCart(){console.log(`Agregando ${this.quantity} unidades del artículo ${this.name} al carrito`)returnArticle}}exportclassServiceextendsProduct{constructor(name, price, quantity, serviceName){super(name, price, quantity);this.serviceName= serviceName;}addToCart(){return`Agregando el servicio ${this.serviceName} al carrito`}}exportclassCartextendsProduct{constructor(){this.products=[]}addProduct(product){this.products.push(product); product.addToCart()}deleteProduct(product){const index =this.products.findIndex(p=> p === product);if(index !==-1)this.products.splice(index,1)}calculateTotal(){const total =this.products.reduce((accum, current)=> accum +(current.price* current.quantity),0)return total
}getProducts(){returnthis.products}}
Mi solución 💚
.
.
.
import{Product}from"./product";exportclassArticleextendsProduct{addToCart(){return`Agregando ${this.quantity} unidades del artículo ${this.name} al carrito`}}exportclassServiceextendsProduct{addToCart(){return`Agregando el servicio ${this.name} al carrito`}}exportclassCart{ products =[]addProduct(product){this.products.push(product)return product.addToCart()}deleteProduct(product){this.products.splice(this.products.indexOf(product),1)}calculateTotal(){returnthis.products.reduce((total,product)=> total +(product.price* product.quantity),0)}getProducts(){returnthis.products}}
Mi solución
import{Product}from"./product";exportclassArticleextendsProduct{addToCart(){return`Agregando ${this.quantity} del artículo ${this.name} al carrito`}}exportclassServiceextendsProduct{addToCart(){return`Agregando el servicio ${this.name} al carrito`}}exportclassCart{constructor(){this.carrito=newArray();}addProduct(product){this.carrito.push(product);return product.addToCart();}deleteProduct(product){let inx;this.carrito.forEach(function(item, index){if(item.name== product.name){ inx = index;}});if(inx !=undefined){this.carrito.splice(inx,1);}}calculateTotal(){let suma =0;this.carrito.forEach(function(item){ suma += item.price*item.quantity;});return suma;}getProducts(){returnthis.carrito;}}
Mi aporte:
|
|
|
|
|
|
|
|
|
|
import{Product}from"./product";exportclassArticleextendsProduct{// Tu código aquí 👈addToCart(){return`Agregando ${this.quantity} unidades del artículo ${this.name}al carrito`;}}exportclassServiceextendsProduct{// Tu código aquí 👈addToCart(){return`Agregando servicio ${this.name} al carrito`;}}exportclassCart{// Tu código aquí 👈 listProduct =[];addProduct(product){this.listProduct.push(product);}deleteProduct(product){this.listProduct=this.listProduct.filter(element=> element.name!== product.name);}calculateTotal(){returnthis.listProduct.reduce((sum, currentElement)=> sum + currentElement.price* currentElement.quantity,0);}getProducts(){returnthis.listProduct;}
Hola, Mi solución al reto
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
import{Product}from"./product";exportclassArticleextendsProduct{addToCart(){return`Agregando ${this.quantity} unidades del artículo ${this.name} al carrito`;}}exportclassServiceextendsProduct{addToCart(){return`Agregando el servicio ${this.name} al carrito`;}}exportclassCart{constructor(){this.products=[];}addProduct(product){ product.addToCart();this.products.push(product);}deleteProduct(product){const index =this.products.findIndex((itemCart)=> itemCart.name=== product.name);if(index <0){thrownewError("El producto no existe en el carrito de compras");}this.products.splice(index,1);}calculateTotal(){let initialValue =0returnthis.products.reduce(function(totalCart, itemCart){const subtotal = itemCart.quantity* itemCart.price; totalCart += subtotal;return totalCart;}, initialValue);}getProducts(){returnthis.products;}}
.
.
.
.
.
.
exportclassArticleextendsProduct{addToCart(){return`Agregando ${this.quantity} unidades del artículo ${this.name} al carrito`}}exportclassServiceextendsProduct{addToCart(){return`Agregando el servicio ${this.name} al carrito`}}exportclassCart{constructor(productList =[]){this.productList= productList;}addProduct(product){this.productList.push(product); product.addToCart();}deleteProduct(product){const deletedProduct =this.productList.findIndex(i=> i === product);this.productList.splice(deletedProduct,1);}calculateTotal(){returnthis.productList.reduce((acum, product)=>(acum +(product.price* product.quantity)),0);}getProducts(){returnthis.productList;}}
import{Product}from"./product";exportclassArticleextendsProduct{constructor(name, price, quantity){super(name, price, quantity)}addToCart(){return`Agregando ${this.quantity} unidades del artículo ${this.name} al carrito`}}exportclassServiceextendsProduct{constructor(name, price, quantity){super(name, price, quantity)}addToCart(){return`Agregando el servicio ${this.name} al carrito`}}exportclassCart{ products =[];addProduct(product){this.products.push(product) product.addToCart()}deleteProduct(product){let result =this.products.findIndex(pro=> pro.name=== product.name)this.products.splice(result,1)}calculateTotal(){let total =0;for(let key inthis.products){const pro =this.products[key]; total += pro.price* pro.quantity;}return total;}getProducts(){returnthis.products;}}
🛡️🛡️Escudo anti-spoilers🛡️🛡️
Mi solución al reto:
import{Product}from"./product.js";exportclassArticleextendsProduct{addToCart(){return`Agregando ${this.quantity} unidades del artículo ${this.name} al carrito`}}exportclassServiceextendsProduct{addToCart(){return`Agregando el servicio ${this.name} al carrito`}}exportclassCart{ shoppingList =[]addProduct(item){this.shoppingList.push(item)return item.addToCart()}deleteProduct(product){let indexToRemove =this.shoppingList.findIndex((item)=> item.name= product.name)if(indexToRemove <0)thrownewError('No se encuentra el producto', product.name)this.shoppingList.splice(indexToRemove,1)}calculateTotal(){let total =0; total =this.shoppingList.reduce((acc, item)=> acc += item.price* item.quantity,0)return total
}getProducts(){returnthis.shoppingList}}