该函数应该创建并显示一个包含数字的数组。缺少一些元素,导致该数组无法显示,因此无法满足“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));
关于javascript - 函数中缺少元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42566022/