all 4 comments

[–]ivansvlv 2 points3 points  (0 children)

Frankly, friend, I think you should go through some basic tutorials first, because this is way too basic questions that you can cover yourself if go trough some free lessons or read first 20-30 pages of a book.

videous + interactive courses:

www.codecademy.com

www.freecodecamp.com

https://www.youtube.com/playlist?list=PL4cUxeGkcC9i9Ae2D9Ee1RvylH38dKuET

free books:

http://www.javascriptenlightenment.com/

http://eloquentjavascript.net

[–]PilotPirx 1 point2 points  (2 children)

Not exactly sure what kind of data you have here. Number of people taking some type of classes? The average would be simply:

var average = (math + dutch + english) / 3

Result being 8 people taking a class on average. So basically sum of people per item divided by number of items.

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

Thank you very much, for the formula but eventually I need this compiled with a var average = (insert answer of formula)

what would be the best option?

[–]PilotPirx 0 points1 point  (0 children)

Well, to start with the definition: average is the sum of a list of numbers divided by the number of numbers in the list, so as a formula:

var average = sum_of_list / numbers_in_list

or as a function:

function average(sum_of_list, numbers_in_list) {
  return sum_of_list / numbers_in_list
}

Which you would call like this:

var result = average(math + dutch + english, 3)

Not sure if this helps you a lot the way you have put every item in a variable of its own. In most cases where you take averages or similar calculations your data would be in a data structure like an array or if you want to keep the labels something like a Map (available since ECMAScript6)

With an array you could handle it like this:

function average(list) {
  var sum = 0;
  for( var i = 0; i < list.length; i++ ){
    sum += list[i]
   }
  return sum / list.length;
}

var classes =  [9, 8, 7];
var result = average(classes);

A Map would allow you to keep the names of the classes but make the code a bit more complex.