[英]Test run on freecodecamp is failing even though code runs successfully from Chrome developer console?
I'm under basic algorithm section of freecodecamp and have to write a function to return the largest numbers in sub-arrays.
我在freecodecamp的基本算法部分下,必须编写一个函数以返回子数组中的最大数字。 Code runs perfectly in Chrome developer console, but all test runs are failing:
代码可以在Chrome开发者控制台中完美运行,但是所有测试运行均失败:
function largestOfFour(arr) {
new_arr = [];[![enter image description here][1]][1]
for(var i = 0; i < arr.length; i++){
var highestNum = Math.max.apply(null, arr[i]);
new_arr.push(highestNum);
};
return new_arr;
};
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Here are the 4 tests that are failing. 这是失败的4个测试。 Also took a screenshot:
还拍了截图:
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]) should return an array.
largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]) should return [27, 5, 39, 1001].
largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].
largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]) should return [25, 48, 21, -3].
You should show the actual error from their output preview. 您应该从其输出预览中显示实际错误。
Try var new_arr = [];
尝试
var new_arr = [];
This returns just fine 这返回就好了
function largestOfFour(arr) {
new_arr = [];
for(var i = 0; i < arr.length; i++){
var highestNum = Math.max.apply(null, arr[i]);
new_arr.push(highestNum);
};
return new_arr;
};
let arr = [[4000, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]
largestOfFour(arr) // [4000, 27, 39, 1001]
The test are most likely failing because the array should not be returned, but rather printed to the console. 测试很可能失败,因为不应返回该数组,而应将其打印到控制台。 Change
return
to console.log
: 将
return
更改为console.log
:
console.log(new_arr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.