[英]Function returning undefined after calling in string
我正在嘗試使用JS制作購物車,而我的任務之一就是創建一個placeOrder函數。
placeOrder()函數接受一個參數,即信用卡號。
如果未收到任何參數,則該函數應打印出“對不起”,我們沒有為您准備的信用卡。
如果收到卡號,此功能應打印出Your total cost is $71, which will be charged to the card 83296759
。 然后,它應該清空購物車數組。
但是,當我在total函數中調用字符串時,返回的值始終未定義。
var cart = []; function getCart() { return cart; } function setCart(c) { cart = c; return cart; } function addToCart(itemName) { var object = { [itemName]: Math.floor(Math.random(1, 100) * 100) }; cart.push(object); console.log(`${itemName} has been added to your cart`); return cart; } function total() { if (cart.length !== 0) { var totalValue = []; for (var i = 0; i < cart.length; i++) { for (var item in cart[i]) { totalValue.push(cart[i][item]); var sum = totalValue.reduce(function(a, b) { return a + b; }, 0); console.log(`The total value is ${sum}`); } } } else { return ("Your shopping cart is empty.") } } function placeOrder(cardNumber) { if (cardNumber === undefined) { return ("Sorry, we don't have a credit card on file for you."); } else { console.log(`Your total cost is $${total()}, which will be charged to the card ${cardNumber}`); cart = []; return cart; } } addToCart("a"); addToCart("be"); addToCart("cart"); placeOrder(14564);
輸出:
Your total cost is $undefined, which will be charged to the card 14564
當cart.length!= 0時,函數total不會返回。您必須返回sum。
if (cart.length !== 0) {
var sum = 0;
for(var i=0; i< cart.length; i++) {
for(var item in cart[i]) {
totalValue.push(cart[i][item]);
sum = totalValue.reduce(function(a, b) {return a + b;}, 0);
console.log(`The total value is ${sum}`);
}
}
return sum;
}
正如@Carcigenicate所說,您應該在if(cart.length!== 0){}語句的結尾處調用return。 請記住,當調用return時,該函數立即停止執行,而不會評估進一步的指令。
您需要確保total
總是返回一個number
,例如:
var cart = [{ "a": 86 }, { "be": 2 }, { "cart": 24 } ]; function total(cart) { return cart .reduce(function(prices, item) { return prices.concat(Object.keys(item).map(k => item[k])); }, []) .reduce(function(sum, price) { return sum += price; }, 0); } console.log(total([])); console.log(total(cart));
然后, placeOrder
的用法如下所示:
function total(cart) { return cart .reduce(function(prices, item) { return prices.concat(Object.keys(item).map(k => item[k])); }, []) .reduce(function(sum, price) { return sum += price; }, 0); } function placeOrder(cardNumber, cart) { if (cardNumber === undefined) { return "Sorry, we don't have a credit card on file for you."; } if (cart.length === 0) { return "You cart is empty"; } return `Your total cost is $${total(cart)}, which will be charged to the card ${cardNumber}`; return []; } console.log(placeOrder(1234, [])); console.log(placeOrder(1234, [{ a: 1 }, { b: 2 }]));
totalValue
的目的是什么,但是您不需要該函數reduce
,只需遍歷項目並求和。 Object.values(cart[i]).pop();
獲得項目的數量Object.values(cart[i]).pop();
。 var sum = 0;
for (var i = 0; i < cart.length; i++) {
var amount = Object.values(cart[i]).pop();
sum += amount;
}
最后,返回該sum
。
var cart = []; function getCart() { return cart; } function setCart(c) { cart = c; return cart; } function addToCart(itemName) { var object = { [itemName]: Math.floor(Math.random(1, 100) * 100) }; cart.push(object); console.log(`${itemName} has been added to your cart`); return cart; } function total() { if (cart.length !== 0) { var sum = 0; for (var i = 0; i < cart.length; i++) { for (var item in cart[i]) { sum += cart[i][item]; } } console.log(`The total value is ${sum}`); return sum; } return -1; // Could be 0, this is up to you. } function placeOrder(cardNumber) { if (cardNumber === undefined) { return ("Sorry, we don't have a credit card on file for you."); } else { var sum = total(); if (sum) { console.log(`Your total cost is $${sum}, which will be charged to the card ${cardNumber}`); } else { console.log("Your shopping cart is empty.") } cart = []; return cart; } } addToCart("a"); addToCart("be"); addToCart("cart"); placeOrder(14564);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.