logo CodeStepByStep logo

partyMaxLevel

Language/Type: JavaScript json objects
Author: Melissa Hovik (on 2018/10/22)

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 maxLevel that takes as a parameter a JavaScript object representing a party of Pokemon as defined, and returns the highest level for the 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" : 12
    },
    { 
        "name" : "Caterpie",
        "nickname" : "Wiggles",
        "type" : ["bug"],
        "weakness" : ["fire"],
        "level" : 1 
    }
]};

then the call maxLevel(myParty) should return the value 30 since that is the highest level in the party of 3 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.

Function: Write a JavaScript function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.