當我在inspect的Chrome源代碼工具中運行此代碼時,它表示第7行(groceryList.forEach(function(number){))的number = undefined。參數正確傳遞並且如果我爲每個項目做一個console.log它顯示正確。與註釋.map部分相同的問題,做與foreach相同的事情。Javascript for each/.map顯示undefined
誰能告訴我我做錯了什麼嗎?
//BUSINESS LOGIC
var alphabatize = function(item1,item2,item3,item4){
groceryList = [item1,item2,item3,item4];
groceryList.sort();
upperGroceryList = []
groceryList.forEach(function(number) {
console.log(number);
upperGroceryList.push(number.toUpperCase());
});
/* upperGroceryList = groceryList.map(function(grocery) {
console.log(grocery);
return grocery.toUpperCase();
}); */
return groceryList;
return upperGroceryList;
};
// FRONT END
$(document).ready(function() {
$("form#items-form").submit(function(event) {
event.preventDefault();
var item1 = $("input#item1").val();
var item2 = $("input#item2").val();
var item3 = $("input#item3").val();
var item4 = $("input#item4").val();
alphabatize(item1,item2,item3,item4);
upperGroceryList.forEach(function(currentItem) {
$("#display-list ul").append("<li>" + currentItem + "</li>");
});
});
})