简体   繁体   English

无法弄清楚为什么我一直不确定

[英]Can't figure out why I keep getting undefined

I'm a beginner so I apologize for the "easy" question.我是初学者,所以我为“简单”的问题道歉。 Thank you.谢谢你。

 let majority = (arr, call) => { let reducedArr = arr.reduce(function(accu, item, index) { if (call(item) >= index) { return true } else { return false } }) } const isOdd = function(num) { return num % 2 === 1; }; console.log(majority([1, 2, 3, 4, 5], isOdd)); // should log: true console.log(majority([2, 3, 4, 5], isOdd)); // should log: false

majority() needs to return something. majority()需要返回一些东西。 Since it doesn't have a return statement, it returns undefined by default.由于它没有return语句,因此默认返回undefined

Your reduce() logic is all wrong, since it's not using the accumulator parameter.您的reduce()逻辑都是错误的,因为它没有使用 accumulator 参数。

From the name and your expected results, I assume that the function is supposed to return whether the call function is true for the majority of elements in the array.根据名称和您的预期结果,我假设 function 应该返回call function 对于数组中的大多数元素是否为真。 So you can use reduce to count the number of true results, by adding 1 to accu whenever it's true.因此,您可以使用reduce来计算真实结果的数量,只要它是真实的,就将accu1 Then you return accu to update the accumulator as it's iterating through the array.然后return accu以更新累加器,因为它正在遍历数组。

You also need to supply the initial value of the accumulator as the second argument to reduce() .您还需要提供累加器的初始值作为reduce()的第二个参数。 Otherwise, it uses the first element of the arr as the initial value.否则,它使用 arr 的第一个元素作为初始值。

Then you need additional code to check if this is the majority.然后你需要额外的代码来检查这是否是大多数。 Compare this total to half the length of the array.将此总数与数组长度的一半进行比较。

 let majority = (arr, call) => { let reducedArr = arr.reduce(function(accu, item, index) { if (call(item)) { accu++; } return accu; }, 0); return reducedArr > arr.length/2; } const isOdd = function(num) { return num % 2 === 1; }; console.log(majority([1, 2, 3, 4, 5], isOdd)); // should log: true console.log(majority([2, 3, 4, 5], isOdd)); // should log: false

You just need to return return reducedArr;你只需要返回return reducedArr; for that piece of code to work how ever i don't think that your logic is correct since you are resetting your accumulator for each element in the array.为了使那段代码正常工作,我认为您的逻辑不正确,因为您正在为数组中的每个元素重置累加器。 if you can edit and add what you are trying to accomplish may be we can help you more.如果您可以编辑和添加您想要完成的内容,我们可以为您提供更多帮助。

after seeing what you are trying to accomplish here is a snippet with some comments that my help you understand it better i know reduce can be tricky when you are learning it but you will get the hang of it.在看到您在这里尝试完成的内容之后,是一个带有一些评论的片段,我可以帮助您更好地理解它

let majority = (arr, call) => {

  let reducedArr = arr.reduce(function( acc, item ) {
    // call(item) returns true if number is odd so there is no need to check it against index
    if ( call(item) ) {
      // if number is odd we increase the odd count in the accumulator
      acc.odd +=1
    } else {
      // if number is even we increase the even count in the accumulator
      acc.even+=1
    }
    return acc;
  }, {odd: 0, even: 0}) //initial accumulator value
  return reducedArr.odd > reducedArr.even; // return true if odd count  is larger than even count
}

const isOdd = function(num) {
  return num % 2 === 1;
};

console.log(majority([1, 2, 3, 4, 5], isOdd)); // should log: true
console.log(majority([2, 3, 4, 5], isOdd)); // should log: false

hope this help.希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在JavaScript中变得未定义,无法弄清为什么 - I am getting undefined in JavaScript and can't figure it out why 变得“未定义”,无法弄清原因 - Getting 'undefined', can't figure out why 我不知道为什么我不断收到此错误 - I can not figure out why I keep getting this error 我的代码中不断出现“未捕获范围”错误,我不知道为什么 - I keep getting an 'uncaught range' error in my code and I can't figure out why 无法弄清楚为什么我不断收到 [ERR_HTTP_HEADERS_SENT] 错误 - Can't figure out why I keep getting [ERR_HTTP_HEADERS_SENT] error 我不断收到错误:TypeError Cannot read property 'getContext' of null 并且不知道为什么 - I keep getting the error: TypeError Cannot read property 'getContext' of null and can't figure out why 我不知道为什么我在这里得到“无法读取未定义的属性'长度'的错误”错误 - I can't figure out why I'm getting the “Cannot read property 'length' of undefined” error here 无法弄清楚为什么我得到“无法读取未定义的属性'0'”错误 - Can't figure out why I'm Getting “Cannot read property '0' of undefined” Error 我不知道为什么状态未定义? - I can't figure out why state is undefined? 不确定的东西,我不知道为什么 - Something is undefined, and I can't figure out why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM