Refaktoryzuj metody obiektu aby działały gdy w tablicy znajdują się obiekty a nie stringi.

0

Nie wiem nawet jak się za to zabrać

const atTheOldToad = {

  potions: [                      
    { name: "Speed potion", price: 460 },  // obiekt 1
    { name: "Dragon breath", price: 780 }, // obiekt 2
    { name: "Stone skin", price: 520 },    // obiekt 3
  ],
  
  getPotions() {
    return this.potions;
  },


  addPotion(newPotion) {
    if (this.potions.includes(newPotion)) {
      return `Error! Potion ${newPotion} is already in your inventory!`;
    }
    this.potions.push(newPotion);
  },
  

  removePotion(potionName) {
    const potionIndex = this.potions.indexOf(potionName);
    if (potionIndex === -1) {
      return `Potion ${potionName} is not in inventory!`;
    }
    this.potions.splice(potionIndex, 1);
  },

  
  updatePotionName(oldName, newName) {
    const potionIndex = this.potions.indexOf(oldName);
    if (potionIndex === -1) {
      return `Potion ${oldName} is not in inventory!`;
    }
    this.potions.splice(potionIndex, 1, newName);
  },
};
1
if (this.potions.includes(newPotion)) {

zamiast .includes możesz użyć metody .find, która przyjmuje funkcję https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

.includes też może działać z obiektami, natomiast porównuje je po referencji, więc { name: "Speed potion", price: 460 } będzie inne od { name: "Speed potion", price: 460 }, bo to dwa różne obiekty. A w .find możesz podać funkcję, która porówna, czy konkretne właściwości się zgadzają.

this.potions.indexOf(potionName);

analogicznie zamiast indexOf możesz użyć findIndex https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

0

Niestety ale dalej nie rozumiem i nie wiem jak to zrobić :( czy ktoś mógłby mi to wytłumaczyć krop po kroku?

1

Ja myślę, że potrzebujesz ogarnąć podstawy i zrozumieć czym są w ogóle obiekty i w jaki sposób działają metody .includes() i .indexOf() na typach prostych (np string) i złożonych (obiekty).

Do tego:

const potion1 = { name: 'test potion', price: 100 };
const potion2 = { name: 'test potion', price: 100 };

Jaki jest rezultat: potion1 === potion2

addPotion(newPotion) {
    if (this.potions.find(potion => potion.name === newPotion.name)) {
      return `Error! Potion ${newPotion.name} is already in your inventory!`;
    }
    this.potions.push(newPotion);
  },

  removePotion(potionName) {
    const potionIndex = this.potions.findIndex(potion => potion.name === potionName);
    if (potionIndex === -1) {
      return `Potion ${potionName} is not in inventory!`;
    }
    this.potions.splice(potionIndex, 1);
  },

  
  updatePotionName(oldName, newName) {
    const potionIndex = this.potions.findIndex(potion => potion.name === potionName);
    if (potionIndex === -1) {
      return `Potion ${oldName} is not in inventory!`;
    }
    this.potions.splice(potionIndex, 1, newName);
  },
};

To tak na szybko.

P.S.: Zacznij od podstaw

1 użytkowników online, w tym zalogowanych: 0, gości: 1