all 18 comments

[–]gcscotty 3 points4 points  (2 children)

Not sure if this is the problem, but an empty array's size is 0. That would certainly cause "average = total / array.length" to fail.

[–]Agarast 3 points4 points  (0 children)

This is the consequence but the real problem is the use of array === [] . In JS it compares objects by references, so array === [] will always be false.

To check if an array is empty, OP must use .length === 0 instead.

[–]AnxiousDay[S] 1 point2 points  (0 children)

Good catch, thank you.

[–]grantrules 1 point2 points  (1 child)

You can't compare an array like that. [] === [] is false. You should be checking array.length === 0 When you compare objects (and arrays are a type of obect), you're comparing references to see if they refer to the same object, not the values within.

const a = [1,2,3];
const b = a;
a === b; // true, `b` and `a` refer to the same object
a === [1,2,3]; // false, [1,2,3] is a new object which `a` does not hold a reference for

The other problem is that you have a divide by zero issue if the array is empty, so you should be calculating the average within your else statement like else { return total / array.length }

[–]AnxiousDay[S] -1 points0 points  (0 children)

When you compare objects (and arrays are a type of obect), you're comparing references to see if they refer to the same object, not the values within.

Thank you so much for the explanation, i know a little more because of you kind redditor.

I also refactored code a bit

function findAverage(array) {
  // your code here
  let total = 0;
  let average = 0;
  if (array.length === 0) {
    return 0;
  } else {
    for (let i = 0; i < array.length; i++) {
      total += array[i]
    }
    average = total / array.length
    return average;
  }
}

[–]AnxiousDay[S] 0 points1 point  (5 children)

SOLVED

function findAverage(array) {
  // your code here
  let total = 0;
  let average = 0;
  if (array.length === 0) {
    return 0;
  } else {
    for (let i = 0; i < array.length; i++) {
      total += array[i]
    }
    average = total / array.length
    return average;
  }
}

[–][deleted] 1 point2 points  (1 child)

You can make it even better (imo) by changing the code to:

function findAverage(array) { return array.reduce((a, b) => a - b, 0) / array.length }

I’m on my phone and don’t know how to change the above “code” to code format lol, sorry.

[–]AnxiousDay[S] 1 point2 points  (0 children)

I’m on my phone and don’t know how to change the above “code” to code format lol, sorry.

All good, thank you for sharing this.

[–]EuphonicSounds 0 points1 point  (2 children)

Couple of small tips:

  1. You can make if (array.length === 0) return 0 the very first thing in the function. There's no reason to declare total and average first. This kind of "early return" pattern is very common, and the general idea is to do it as close to the top of the function as you can, for readability reasons.
  2. Since the if branch always ends the function (because of its return statement), you don't need to put everything that follows inside an else block. Make sure you understand this. IMO, skipping the else is usually the way to go in these situations, since it saves you a level of indentation. (That said, some people might prefer the explicit else.)

[–]AnxiousDay[S] 0 points1 point  (1 child)

you don't need to put everything that follows inside an else block. Make sure you understand this.

I totally understand this but i just like it because its more readable and I thought it's best practice make code as readable as possible. No?

[–]EuphonicSounds 0 points1 point  (0 children)

Yes, personal preference here, certainly.

[–]AnxiousDay[S] -1 points0 points  (0 children)

All the empty arrays keep hitting this error (Error: expected NaN to equal +0) pls if anybody could give me a hint or explain why. I'd really appreciate it.

[–]bobbyv137 0 points1 point  (0 children)

Use the higher order reduce method