Skip to content

Conversation

@CheezItMan
Copy link

This method will calculate the average of an array of grades.

@CheezItMan CheezItMan changed the title calculate average method PR-1 Feb 6, 2020
@@ -0,0 +1,14 @@

const calculateAverage = function calculateAverage(grades) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a one-off single comment!

Copy link

@NatalieTapias NatalieTapias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM


let avg = total / grades.length;

return avg;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like what you've done here.

Copy link

@Galaxylaughing Galaxylaughing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

const calculateAverage = function calculateAverage(grades) {

let total = 0;
for (let i = 0; i < grades.length; i++) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could put grades.length into a variable to avoid recalculating it every loop

total += grades[i];
}

let avg = total / grades.length;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does avg end up as an integer or a float? If all the grades are whole numbers, it might be an integer, but you probably want to force it to do float math

total += grades[i];
}

let avg = total / grades.length;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avg could also be const instead of a let


let avg = total / grades.length;

return avg;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of storing the average as a variable, you could return the calculation return value directly

Suggested change
return avg;
return total / grades.length;

total += grades[i];
}

let avg = total / grades.length;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let avg = total / grades.length;
let avg = total / parseFloat(grades.length);

Copy link

@raisahv raisahv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved


let avg = total / grades.length;

return avg;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can delete line 9 and just do:

Suggested change
return avg;
return total / grades.length;

total += grades[i];
}

let avg = total / grades.length;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming avg to avgGrades to be clearer.

return avg;
};

export default calculateAverage; No newline at end of file

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

return avg;
};

export default calculateAverage; No newline at end of file

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to add a newline here

for (let i = 0; i < grades.length; i++) {
total += grades[i];
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really clean and readable. well sone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.