fork download
  1. // Define a class for the hat store
  2. class HatStore {
  3. constructor() {
  4. this.hats = [];
  5. }
  6.  
  7. // Method to add a hat to the store
  8. addHat(hat) {
  9. this.hats.push(hat);
  10. }
  11.  
  12. // Method to remove a hat from the store
  13. removeHat(hat) {
  14. const index = this.hats.indexOf(hat);
  15. if (index !== -1) {
  16. this.hats.splice(index, 1);
  17. }
  18. }
  19.  
  20. // Method to get all hats in the store
  21. getAllHats() {
  22. return this.hats;
  23. }
  24. }
  25.  
  26. // Define a class for a hat
  27. class Hat {
  28. constructor(name, price, color) {
  29. this.name = name;
  30. this.price = price;
  31. this.color = color;
  32. }
  33. }
  34.  
  35. // Create an instance of the hat store
  36. const hatStore = new HatStore();
  37.  
  38. // Create some hats and add them to the store
  39. const hat1 = new Hat("Baseball Cap", 20, "Black");
  40. const hat2 = new Hat("Beanie", 15, "Blue");
  41. const hat3 = new Hat("Fedora", 30, "Brown");
  42.  
  43. hatStore.addHat(hat1);
  44. hatStore.addHat(hat2);
  45. hatStore.addHat(hat3);
  46.  
  47. // Get all hats in the store
  48. const allHats = hatStore.getAllHats();
  49. console.log(allHats);
  50.  
  51. // Remove a hat from the store
  52. hatStore.removeHat(hat2);
  53.  
  54. // Get all hats in the store again
  55. const updatedHats = hatStore.getAllHats();
  56. console.log(updatedHats);// your code goes here
Success #stdin #stdout 0.03s 18804KB
stdin
Standard input is empty
stdout
[object Object],[object Object],[object Object]
[object Object],[object Object]