JS如何给对象添加价格并通过参数传递

JS how to add a price to an object and pass it by an argument

我是编码新手,收到了这个问题,但我不确定如何更正它。有人有什么建议吗?

参数产品将是一个如下所示的对象:

  { type: 'Tofu slices' }

向此对象添加价格 属性 并将其值设置为作为参数传入的价格。然后 return 对象。

这是我想出的答案;

function addPriceToProduct (product, price) {
product.price = price
return product.price
}

我的回答是运行反对;

describe("addPriceToProduct", () => {
it("adds a price property to the passed product set to the passed price", () => {
  const product = {
    type: "Tofu slices"
  };
  let newProduct = addPriceToProduct(product, 1.25);
  expect(newProduct).to.eql({ type: "Tofu slices", price: 1.25 });
  newProduct = addPriceToProduct(product, 1.35);
  expect(newProduct).to.eql({ type: "Tofu slices", price: 1.35 });
});
});

只是return产品(对象)不是product.price(价格属性的价值)

function addPriceToProduct (product, price) {
    product.price = price;
    return product;
}