此函数应创建并显示带有数字的数组。缺少一些元素,该元素阻止此数组显示并因此无法满足“ console.log”假设:



function createArray(number) {
    var newArray = [/*10*/];

    for(var counter = 1; counter <= number; counter++) {
        newArray.push(counter);
    }
}

console.log("table with numbers up to 6 = " + createArray(6));
console.log("table with numbers up to 1 = " + createArray(1));
console.log("Testing negatives (should display an empty array) " + createArray(-6));
console.log("Testing 0 (should display an empty array) " + createArray(0));





您能分析一下并提供一些反馈吗?

最佳答案

您在函数末尾缺少return newArray;。如果没有return语句,则该函数默认返回undefined



function createArray(number) {
    var newArray = [/*10*/];

    for(var counter = 1; counter <= number; counter++) {
        newArray.push(counter);
    }
    return newArray;
}

console.log("table with numbers up to 6 = " + createArray(6));
console.log("table with numbers up to 1 = " + createArray(1));
console.log("Testing negatives (should display an empty array) " + createArray(-6));
console.log("Testing 0 (should display an empty array) " + createArray(0));

09-25 18:43