visylvius
visylvius

Reputation: 65

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.

Upvotes: 2

Views: 83

Answers (1)

HashPsi
HashPsi

Reputation: 1391

Your function is missing a return statement:

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

Upvotes: 4

Related Questions