我正在致力于一个问题,使我创建一个对象和两个功能,以创建一个模拟店面的搜索引擎优化网站。方向如下:
我相信我已经正确地创建了一个对象,但是函数是关闭的。我尝试过调整参数,产品库存保持在相同的@ 6;考虑到我传递的参数,应该是10。我尝试添加numOfProducts和产品库存,但没有效果。相反,它只是打印出来6。我非常感谢您能提供的任何帮助。谢谢
var product = {
name: "Skull Candy Headphones",
inventory: 6,
unit_price: 49.99
}
function addInventory(product, numOfProducts) {
product.inventory + numOfProducts;
console.log(numOfProducts + " "+ product.name + ' added to inventory');
}
addInventory(product, 4);
发布于 2019-06-05 02:52:15
您的意思是添加product.inventory + numOfProducts然后设置product.inventory的值吗?
您正在正确地执行数学部分(添加product.inventory + numOfProducts),但实际上没有设置结果的值。
你需要这样做:
product.inventory = product.inventory + numOfProducts;
var product = {
name: "Skull Candy Headphones",
inventory: 6,
unit_price: 49.99
}
function addInventory(product, numOfProducts) {
product.inventory = product.inventory + numOfProducts;
console.log(numOfProducts + " "+ product.name + ' added to inventory. product.inventory is ' + product.inventory);
}
addInventory(product, 4);
https://stackoverflow.com/questions/56453678
复制相似问题