/*
Gradebook from Names and Scores
I worked on this challenge [by myself, with: Meagan Elizabeth]
This challenge took me [0.75] hours.
You will work with the following two variables.  The first, students, holds the names of four students.
The second, scores, holds groups of test scores.  The relative positions of elements within the two
variables match (i.e., 'Joseph' is the first element in students; his scores are the first value in scores.).
Do not alter the students and scores code.
*/

var students = ["Joseph", "Susan", "William", "Elizabeth"]

var scores = [ [80, 70, 70, 100],
               [85, 80, 90, 90],
               [75, 70, 80, 75],
               [100, 90, 95, 85] ]






// __________________________________________
// Write your code below.
//Release 1

var gradebook = {
  
};


//Release 2 and 3
for (var i in students) {
  gradebook[students[i]] = {};
  gradebook[students[i]]['testScores'] = scores[i];
}

//console.log(gradebook)

// Release 4

gradebook.addScore = function (name, score) {
  gradebook[name]['testScores'].push(score);
}
// Release 5
gradebook.getAverage = function(name){
  return average(gradebook[name]['testScores'])
};
console.log(gradebook.getAverage("Joseph"))
// Release 6
function average(array){
  var sum = 0
  for (var i in array){
    sum += array[i]
  }
  var avg= sum/array.length
  return avg;
}

console.log(average ([1,2,3,4]))







// __________________________________________
// Refactored Solution
//Release 1

var gradebook = {
  
};


//Release 2 and 3
for (var i in students) {
  gradebook[students[i]] = {};
  gradebook[students[i]]['testScores'] = scores[i];
}

//console.log(gradebook)

// Release 4

gradebook.addScore = function (name, score) {
  gradebook[name]['testScores'].push(score);
}
// Release 5
gradebook.getAverage = function(name){
  return average(gradebook[name]['testScores'])
};
console.log(gradebook.getAverage("Joseph"))
// Release 6
function average(array){
  var sum = array.reduce(function(a,b){
    return a + b;
  });
  var avg= sum/array.length
  return avg;
}

console.log(average ([1,2,3,4]))







// __________________________________________
// Reflect
/*
What did you learn about adding functions to objects?
How did you iterate over nested arrays in JavaScript?
Were there any new methods you were able to incorporate? If so, what were they and how did they work?
*/








// __________________________________________
// Test Code:  Do not alter code below this line.


function assert(test, message, test_number) {
  if (!test) {
    console.log(test_number + "false");
    throw "ERROR: " + message;
  }
  console.log(test_number + "true");
  return true;
}



assert(
  (gradebook instanceof Object),
  "The value of gradebook should be an Object.\n",
  "1. "
)

assert(
  (gradebook["Elizabeth"] instanceof Object),
  "gradebook's Elizabeth property should be an object.",
  "2. "
)

assert(
  (gradebook.William.testScores === scores[2]),
  "William's testScores should equal the third element in scores.",
  "3. "
)

assert(
  (gradebook.addScore instanceof Function),
  "The value of gradebook's addScore property should be a Function.",
  "4. "
)

gradebook.addScore("Susan", 80)

assert(
  (gradebook.Susan.testScores.length === 5
   && gradebook.Susan.testScores[4] === 80),
  "Susan's testScores should have a new score of 80 added to the end.",
  "5. "
)

assert(
  (gradebook.getAverage instanceof Function),
  "The value of gradebook's getAverage property should be a Function.",
  "6. "
)

assert(
  (average instanceof Function),
  "The value of average should be a Function.\n",
  "7. "
)
assert(
  average([1, 2, 3]) === 2,
  "average should return the average of the elements in the array argument.\n",
  "8. "
)

assert(
  (gradebook.getAverage("Joseph") === 80),
  "gradebook's getAverage should return 80 if passed 'Joseph'.",
  "9. "
)
@@ -2219,163 +2219,684 @@
 ts?%0A
-How did you iterate over nested arrays in JavaScript?%0AWere there any new methods you were able to incorporate? If so, what were they and how did they work?
+In this specific case we used the dot notation to create a property and then its value as a function.%0AThis was similar to simply creating a new property but we add a function as a value here.%0AHow did you iterate over nested arrays in JavaScript?%0AUsing a for loop and then we use ( var i in object) in order to iterate!%0AWere there any new methods you were able to incorporate? If so, what were they and how did they work?%0AThe reduce method that is built-in for JavaScript in the documentation. It allowed me to sum the elements in the array%0Aas opposed to coming up with a more complicated means of doing so the reduce method allows you to add a function to add( in my%0Acase a+b)
 %0A*/%0A

Add reflection to gradebook challenge