我正在使用code.org,并且尝试从数组中返回该项目,但是在第34行收到错误,可能的食物[selectedFood]未定义。我不明白为什么,这是阻止食物选择区起作用的原因吗?我应该如何正确定义selectedFood?

setScreen("BaseScreen");
onEvent("selectionbutton", "click", function( ) {
    setScreen("food_decision_screen");
});
var selections = [true, true, true];
var possibleFoods = [
    {name:"Raw pasta prima vera", info:{recipe:"", prepTime:{total:20,cook:0,  prep:20}, image:""}},
    {name:"Zucchini chips",       info:{recipe:"", prepTime:{total:55,cook:30, prep:25}, image:""}}
];

var possibleFoods;
onEvent("preference_finished_button", "click", function( ) {
    selections = [
        getChecked("radio_buttonhot"),
        getChecked("radio_buttonvege"),
        getChecked("radio_button0-30")
    ];

    if (selections[0] == false) {
        if (selections[1] == false) {
            if (selections[2] == true) {
                selectedFood = 1;
            }
        }
    }

    setText("t_textOutputFoodInfo", "total time: " +
    possibleFoods[selectedFood].info.prepTime.total);
    setText("t_textOutput_foodSelectedName", "name: " +
    possibleFoods[selectedFood].name);
});


selectedFood未定义。不确定如何定义。

最佳答案

没有声明selectedFood变量。此外,如果条件不一定总是正确的,那么您将在嵌套内部分配它。

尝试这个:

在if语句之前,用一个初始值在外部声明selectedFood。

var selectedFood = 0;

10-02 16:46