logo CodeStepByStep logo

partyCountOf

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

  1. A JavaScript object called data representing a party of Pokemon as defined
  2. A string parameter called elementType representing a Pokemon element type (e.g. "electric")

Your function should return the count of how many Pokemon occur in data sharing the given elementType, ignoring letter-casing.

For example, if the following object is defined:

let myParty = { "party" : [
    { 
        "name" : "Magikarp",
        "nickname" : "Flip",
        "type" : ["water"],
        "weakness" : ["electic", "grass"],
        "level" : 1
    },
    { 
        "name" : "MAGIKARP",
        "nickname" : "Flop",
        "type" : ["water"],
        "weakness" : ["electic", "grass"],
        "level" : 1
    { 
        "name" : "mAGIKARP",
        "nickname" : "Splish",
        "type" : ["water"],
        "weakness" : ["electic", "grass"],
        "level" : 1
    { 
        "name" : "MaGiKaRp",
        "nickname" : "Splash",
        "type" : ["water"],
        "weakness" : ["electic", "grass"],
        "level" : 1
    },
    { 
        "name" : "magikarp",
        "nickname" : "Sploosh",
        "type" : ["water"],
        "weakness" : ["electic", "grass"],
        "level" : 1
    },
    { 
        "name" : "Gyarados",
        "nickname" : "Ikuchi",
        "type" : ["water", "dragon"],
        "weakness" : ["electic"],
        "level" : 100
    }
]};

then the call pokemonCountOf(myParty, "water") should return 6 and the call pokemonCountOf(myParty, "DRAGON") should return 1.

You may assume that the passed data is non-null but if the "party" key is missing, your function should throw an exception with the message, "Passed object must have 'party' key".

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.