JavaScript Mini-Max Sum-HackerRank网站的挑战 [英] JavaScript Mini-Max Sum - Challenge from HackerRank website

查看:106
本文介绍了JavaScript Mini-Max Sum-HackerRank网站的挑战的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是挑战:

https://www.hackerrank.com/challenges/mini-max-sum/problem

尽管我的回答是相同的符合预期结果的数字,我做错了某件事,因为我的答案被拒绝了。
我该如何解决?

Despite my answer is returning the same number that matches the expected result, I have done something wrong because my answer has been rejected. How can I solve it?

这是我尝试过的解决方案:

Here is the solution I had tried:

function miniMaxSum(arr) {   

  var arrClone1 = arr.slice() 
  var arrClone2 = arr.slice() 

  var arrMinor = arrClone1.sort(function(a, b){return a - b;})
  arrMinor.pop()

  var arrMajor = arrClone2.sort(function(a, b){return b - a;})
  arrMajor.pop()

  function getSum(a, b) {
    return a + b;
  }

  var result1 = arrMinor.reduce(getSum) 
  var result2 = arrMajor.reduce(getSum)    

  console.log(`${result1} ${result2}`) // it is returning: 10 14 


推荐答案

我找到了答案。我注意到将函数参数命名为输入而不是 arr是强制性的。这就是为什么尽管代码返回了正确的结果,答案仍被HackerRank平台拒绝的原因

I found the answer. I noticed that was mandatory to name the function argument as 'input' instead of 'arr'. That's why the answer was rejected by the HackerRank platform despite the code returned the right result

就像这样

function miniMaxSum(input) {   //'input' not 'arr'    
  var arrClone1 = input.slice()   //'input' not 'arr'
  var arrClone2 = input.slice()   //'input' not 'arr'

//... rest of the code omitted

这篇关于JavaScript Mini-Max Sum-HackerRank网站的挑战的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆