Conversation
| @@ -0,0 +1,14 @@ | |||
|
|
|||
| const calculateAverage = function calculateAverage(grades) { | |||
|
|
||
| let avg = total / grades.length; | ||
|
|
||
| return avg; |
| const calculateAverage = function calculateAverage(grades) { | ||
|
|
||
| let total = 0; | ||
| for (let i = 0; i < grades.length; i++) { |
There was a problem hiding this comment.
you could put grades.length into a variable to avoid recalculating it every loop
| total += grades[i]; | ||
| } | ||
|
|
||
| let avg = total / grades.length; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
avg could also be const instead of a let
|
|
||
| let avg = total / grades.length; | ||
|
|
||
| return avg; |
There was a problem hiding this comment.
instead of storing the average as a variable, you could return the calculation return value directly
| return avg; | |
| return total / grades.length; |
| total += grades[i]; | ||
| } | ||
|
|
||
| let avg = total / grades.length; |
There was a problem hiding this comment.
| let avg = total / grades.length; | |
| let avg = total / parseFloat(grades.length); |
|
|
||
| let avg = total / grades.length; | ||
|
|
||
| return avg; |
There was a problem hiding this comment.
Can delete line 9 and just do:
| return avg; | |
| return total / grades.length; |
| total += grades[i]; | ||
| } | ||
|
|
||
| let avg = total / grades.length; |
There was a problem hiding this comment.
Consider renaming avg to avgGrades to be clearer.
| return avg; | ||
| }; | ||
|
|
||
| export default calculateAverage; No newline at end of file |
| return avg; | ||
| }; | ||
|
|
||
| export default calculateAverage; No newline at end of file |
| for (let i = 0; i < grades.length; i++) { | ||
| total += grades[i]; | ||
| } | ||
|
|
There was a problem hiding this comment.
This is really clean and readable. well sone
This method will calculate the average of an array of grades.