This problem with work with a JSON-formatted object representing a "party" (collection) of Pokemon
objects with the following schema:
{ "party" : [
{ "name" : String, "nickname" : String, "type" : String[], "weakness" : String[], "level" : Number },
{ "name" : String, "nickname" : String, "type" : String[], "weakness" : String[], "level" : Number },
...
]}
Each party (an array corresponding to the "party" key) contains Pokemon objects, each with a name
(string), nickname (string), type (string array), weakness (string array), and level (Number).
The type and weakness arrays represent the (possibly-multiple) "type" elements and "weakness"
elements of the Pokemon. While not needed to solve this problem, a Pokemon's type determines
whether it is strong against another Pokemon
with a weakness for that first Pokemon's type. The level represents how many "experience points"
the Pokemon has accumulated and is in a range from 1 to 100 (inclusive).
Write a function named avgLevel
that takes as a parameter a JavaScript object
representing a party of Pokemon as defined, and returns the average level for all Pokemon in the party array.
For example, if the following object is defined:
let myParty = { "party" : [
{
"name" : "Pikachu",
"nickname" : "Sparky",
"type" : ["electric"],
"weakness" : ["ground"],
"level" : 30
},
{
"name" : "Squirtle",
"nickname" : "Bubbles",
"type" : ["water"],
"weakness" : ["electic", "grass"],
"level" : 14
},
{
"name" : "Caterpie",
"nickname" : "Wiggles",
"type" : ["bug"],
"weakness" : ["fire"],
"level" : 1
},
{
"name" : "Charizard",
"nickname" : "Drogon",
"type" : ["water"],
"weakness" : ["electic", "grass"],
"level" : 100
}
]}
then the call avgLevel(myParty)
should return the value 36.25 since that is the average of the levels
for the party of 4 Pokemon. If any Pokemon objects have a level value that is less than 1 or greater than 100,
your function should throw an exception with the message,
"All objects in party array must have level between 1 and 100 (inclusive)".
You may assume that the passed object is non-null but if the "party" key is missing or has an empty array as its value,
your function should throw an exception with the message: "Passed object must have 'party' key with non-empty array value".
You may assume all of the keys of each Pokemon object in the "party" array are defined and have the datatype
specified in the provided schema.