-2
function filterEvenElements(arr) {
  for (var i = 0; i < arr.length; i++) {
    var newArr = [];

    if (arr[i] % 2 !== 0) {
      newArr.push(arr[i]);
    }

    return newArr;
  }

  console.log(newArr);
}

var output = filterEvenElements([2, 3, 4, 5, 6]);
console.log(output); // --> [2, 4, 6]

when I return I return newArr it's just an empty array? Why is this? I cannot figure it out

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Brixsta
  • 605
  • 12
  • 24
  • 5
    You're declaring a new empty array on each iteration of the loop, the function quits on the first iteration (and even if it didn't, you're still declaring a new empty array on the next iteration). – David Thomas Mar 12 '21 at 20:11
  • 5
    Also, `return newArr;` is inside the `for` loop so gets executed on the 1st iteration. – 001 Mar 12 '21 at 20:11
  • 1
    @DavidsaysreinstateMonica We need canonical dupes for both these errors, we get them every day. – Barmar Mar 12 '21 at 20:13
  • 1
    @Barmar [Does return stop a loop?](https://stackoverflow.com/q/11714503) is one of them – VLAZ Mar 12 '21 at 20:15

3 Answers3

3

You need to move the return newArr outside of the for loop, as well as the newArray declaration.

If you don't move the return statement outside of the for loop, it would immediately return after one iteration. This is also the reason why it's returning an empty array for you.

Also you need to move the declaration of the variable above the for loop, because it would otherwise generate an empty array each iteration.

function filterEvenElements(arr) {
  // to here
  var newArr = [];
  for(var i=0; i < arr.length; i++) {
    if(arr[i] % 2 !== 0) {
      newArr.push(arr[i]);
    }
  }

  // and here
  return newArr;
}

var output = filterEvenElements([2, 3, 4, 5, 6]);
console.log(output); // --> [2, 4, 6]
voe
  • 31
  • 3
  • 1
    While you're correct, would you care to explain why? That way the OP - and future visitors - may learn something useful and valuable. – David Thomas Mar 12 '21 at 20:16
  • @DavidsaysreinstateMonica I have updated my answer accordingly and provided a more thorough explanation. – voe Mar 12 '21 at 20:19
2

The problem is that newArr is declared in each iteration and will be returned after the first one in this case.

So what will happen in the first iteration is newArr set to [], arr[0] is even so it won't be pushed, and newArr will be returned empty, hence, the function will end. Here are some logs:

function filterEvenElements(arr) {
  for (var i = 0; i < arr.length; i++) {
    console.log("iteration: ", i);
    var newArr = [];
    console.log("newArr before condition: ", newArr);
    console.log("condition: ", arr[i] % 2 !== 0);
    if (arr[i] % 2 !== 0) {
      newArr.push(arr[i]);
    }
    console.log("newArr after condition: ", newArr);
    return newArr;
  }
}

var output = filterEvenElements([2, 3, 4, 5, 6]);
console.log("function result: ", output);

You need to define newArr at the beginning of the function, fill the odd numbers in the for-loop until i reaches the length of the array, and return the result of the function:

function filterEvenElements(arr) {
  const newArr = [];    
  for(var i=0; i < arr.length; i++) {
    if(arr[i] % 2 !== 0) {
      newArr.push(arr[i]);
    }
  }
  return newArr;
}

const output = filterEvenElements([2, 3, 4, 5, 6]);
console.log(output);

Another way using .filter:

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

function filterEvenElements(arr) {
  return arr.filter(e => e % 2 !== 0);
}

const output = filterEvenElements([2, 3, 4, 5, 6]);
console.log(output);
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
0

I refactored your code a bit:

  1. hoist newArr before the loop.(i.e, move the array declaration to the beginning of the function scope before the loop starts),

  2. the return statement should be placed after the for loop finishes or else the function will return after the first iteration.

    function filterEvenElements(arr) {
      const newArr = [];
      for(let i=0; i < arr.length; i++) {
        if(arr[i] % 2 !== 0) {
          newArr.push(arr[i]);
        }
    }
    return newArr;
    }
    
    var output = filterEvenElements([2, 3, 4, 5, 6]);
    console.log(output); // --> [2, 4, 6]
    
David Thomas
  • 249,100
  • 51
  • 377
  • 410
Ran Turner
  • 14,906
  • 5
  • 47
  • 53