简体   繁体   中英

Why am i getting undefined with this filter function?

I'm still somewhat new to programming, I've been messing with this code for an hour and still can't figure out why its responding with undefined. I've tried it two different ways. 1st method: var myArr = [1,2,3,4,5];

function oddBall(arr) {
  var oddNumbers = arr.filter(function(x) {
    return x % 2 === 1;
  });
}

console.log(oddBall(myArr));

2nd Method:

function oddBall(arr) {
 arr = arr.filter(function(x) {
  return x % 2 === 1;
 });
}

oddBall([1, 2, 3, 4, 5]);

I know that the filter method is working, but I'm stumped as to why its returning undefined. Any help would be appreciated. Thank you for your time.

Your function is missing a return statement:

function oddBall(arr) {
  return arr.filter(function(x) {
    return x % 2 === 1;
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM