Need Javascript help!

Been working on this challenge here. I know the code is probably overcomplicated. I think I’m on the right track. I just lose track of where the data is going… if that makes sense.

"Create a function called averageArray that takes in an array of numbers and a value of either ‘evens’ or ‘odds’ and, depending on which the function is asked to evaluate, returns either the average of all the even numbers in the array, or all of the odd numbers in the array. "

So, what I interpret from that is that I need to

  1. Create a new function called “averageArray”
  2. Said function needs two parameters, an array and a value of either evens or odds.
  3. Use a conditional statement to determine which set of code to execute. One for evens, one for odds.
  4. Filter this passed in array and create a new array with just the evens/odds
  5. Sum the evens/odds
  6. Divide the sum by the length of the input array
  7. return the average.

I’m curious as to whether I misunderstood the question, or if it was a coding error.

Here’s my code.

var arr1 = [15,17,22,4,3,35,11,12,38,8];

// Create a function that will average all the even or odd numbers in an array.

function averageArray(array, evensOrOdds){

  var avg = 0;                                                                                                 // return var declared

  if (evensOrOdds === "evens") {                                                   //conditional set. 
        var evens = array.filter(num => num % 2 === 0);     // % operator to sort that numbers
        avg = (evens) => evens.reduce((a, b) => a + b) / evens.length; 
        return avg;                                                                                  // output sum
  } else {
        var odds = array.filter(num => num % 2 === 1);
        avg = (odds) => odds.reduce((a, b) => a + b) / odds.length;
        return avg;
  }
}

if I’m being picky, this statement should say instead
Divide the sum by the length of the new array (of evens or odds)

Edit: did you notice that the code you run for evens is exactly the same as the code you run for odds except for one value? Why not make that one value a variable and the code for evens and odds can be a single function that takes that variable and uses it.

So instead of two sets of code, unify the filter and the average parts
array.filter(num => num%2 === value); // where value is either 0 or 1 depending on the type of ‘evensOrOdds’

You’ve never stated what kind of error you were getting and which challenge this is. However, by looking at your code, I see that you are returning a function that computes average instead of numeric average value. If you are getting any error, that is the likely place.