[英]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
来计算真实结果的数量,只要它是真实的,就将accu
加1
。 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.