var stars = [0, 0, 2, 3, 5], count = 0, sum = 0; stars.forEach(function(value, index){ count += value; sum += value * (index + 1); }); console.log(sum / count);