logo CodeStepByStep logo

partyChoosePokemon

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 choosePokemon that takes the following two parameters:

  • A JavaScript object called data representing a party of Pokemon as defined
  • A string parameter called opponent representing a single Pokemon object

Your function should return the "best choice" in the party array of Pokemon objects in data defined by the Pokemon object having the highest level, breaking ties by how many strings occur in the "type" array that match strings in the opponent object's "weakness" array (you can assume there are no duplicates in any Pokemon object's type or weakness arrays). String comparison should be done ignoring letter-casing. Any remaining ties should be broken by whatever Pokemon object occurs earliest in the party array.

For example, if the following object is defined:

let myParty = { "party" : [
    { 
        "name" : "Squirtle",
        "nickname" : "Bubbles",
        "type" : ["water"],
        "weakness" : ["electric", "grass"],
        "level" : 12 
    },
    { 
        "name" : "Charmander",
        "nickname" : "Charmy",
        "type" : ["fire"],
        "weakness" : ["water"],
        "level" : 20
    },
    { 
        "name" : "Lanturn",
        "nickname" : "Magic Lightbulb",
        "type" : ["electric", "water"],
        "weakness" : ["ground", "grass"],
        "level" : 10 
    }
]};

let opponent = {
    "name" : "Moltres",
    "nickname" : "Moltres the Legendary Fire Bird",
    "type" : ["fire", "flying"],
    "weakness" : ["water", "electric"],
    "level" : 40 
};

then the call pokemonOfType(myParty, opponent) should return the object for "Charmander" since it has the highest level of all Pokemon objects in myParty. If all three Pokemon objects in myParty instead had the same level (e.g. 40) then the call pokemonOfType(myParty, opponent) should return the object corresponding to "Lanturn" since it is the Pokemon that has the most strings in its type array that match strings in the weakness array of the opponent ("water" and "electric").

You may assume that the passed data 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 and the opponent parameter 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.