skip to Main Content
function evensOnly(arr) {
  // your code here
  
  const arrEven = arr.filter((arr) => {
    if (arr % 2 === 0) {
      return arr;
    }
    console.log(arrEven);
  });
}
// test
console.log(evensOnly([3, 6, 8, 2])); /// [6, 8, 2]

I got error that ‘cannot access "arrEven" before initializtion’

SyntaxError: Missing initializer in const declaration

4

Answers


  1. you need to declare the arrEven variable before using it inside the filter method

    function evensOnly(arr) {
      // Declare the variable before using it
      const arrEven = arr.filter((num) => {
        if (num % 2 === 0) {
          return num;
        }
      });
    
      // Return the filtered array
      return arrEven;
    }
    
    // Test
    console.log(evensOnly([3, 6, 8, 2])); // Output: [6, 8, 2]
    

    In JavaScript, when you use const to declare a variable, it must be initialized (assigned a value) at the time of declaration. In the original code, arrEven was being used inside the filter method before declaring it, which caused the "Missing initializer in const declaration" error. By declaring arrEven before the filter method, the error is resolved, and the code works as expected.

    Login or Signup to reply.
  2. The console.log() call shouldn’t be inside the filter callback function. The variable isn’t assigned until filter() returns.

    You also shouldn’t be calling console.log() in the function at all. Your code expects evensOnly to return the filtered array, which is logged by the caller.

    You shouldn’t return the array element. The callback function is supposed to return a boolean value. If the array contains 0, return arr will return this, but 0 is treated as false, not true, so it won’t be included in the result. The comparison returns a boolean, so just return that.

    And it’s confusing to use the same variable for the array being filtered and the parameter of the callback function. Use different names.

    function evensOnly(arr) {
      const arrEven = arr.filter((el) => {
        return el % 2 === 0
      });
      return arrEven;
    }
    // test
    console.log(evensOnly([3, 6, 8, 2])); /// [6, 8, 2]
    Login or Signup to reply.
  3. The error you’re encountering, "cannot access ‘arrEven’ before initialization," is because you’re trying to access the arrEven variable inside the filter callback function before it has been properly initialized. To fix this issue, move the console.log(arrEven) statement outside the filter callback function.

    function evensOnly(arr) {
      const arrEven = arr.filter((num) => {
        return num % 2 === 0;
      });
    
      console.log(arrEven);
    }
    
    // test
    evensOnly([3, 6, 8, 2]); // [6, 8, 2]
    Login or Signup to reply.
  4. Others have correctly identified the primary issue with your code, which is that you’re attempting to access a variable within its declaration.

    Furthermore, since 0 is falsy, you can simplify this a bit further with:

    function evensOnly(arr) {
      return arr.filter(el => !(el % 2));
    }
    
    //execution
    console.log(evensOnly([3, 6, 8, 2])); /// [6, 8, 2]

    I.e., you don’t need to capture the results of your .filter() in a temporary arrEven variable, and you also don’t need to explicitly compare with === 0.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search