简体   繁体   English

即使代码已从Chrome开发者控制台成功运行,在freecodecamp上的测试运行仍然失败?

[英]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.

相关问题 jQuery ajax-即使成功运行也会运行错误 - jQuery ajax - error runs even though it ran successfully 即使显示的时间正确显示,freeCodeCamp Pomodoro Clock 也没有通过测试 - freeCodeCamp Pomodoro Clock not passing test even though displayed time shows correctly 为什么这个正则表达式不起作用即使它是有效的并且在测试代码上运行良好 - Why doesn't this regex work Even though it is valid and runs fine on test code 我的JavaScript第14行( <script> inside HTML) code will not run automatically, It will only run in the chrome developer console manually - Line 14 of my JavaScript(<script> inside HTML) code will not run automatically, It will only run in the chrome developer console manually firefox Web开发人员中的奇怪JS控制台错误(虽然是关于chrome的…? - Odd JS console error in firefox web developer (about chrome though…?) Chrome扩展程序:Chrome扩展程序中未定义的变量,即使控制台中存在该变量 - Chrome extensions: variable undefined in chrome extension even though it exists in console 从 freecodecamp 了解代码的复杂性 - understanding the complexity of the code from freecodecamp 从chrome开发者控制台调用javascript函数 - Call javascript function from chrome developer console 开玩笑:spyOn 测试失败,即使(异步)function 正在执行 - Jest: spyOn test failing even though (async) function is executing 为什么即使我使用 chrome 浏览器,赛普拉斯在 GUI(柏树打开)中的测试通过但在命令行(柏树运行)中失败? - Why does Cypress test in GUI(cypress open) pass but fails in command line(cypress run) even though I using chrome browser?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM