logo CodeStepByStep logo

vocabLongestWord

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

This problem will work with JSON-formatted data representing a "vocabulary set" of terms and definitions in the following JSON schema:

{ 
    "setname" : String,
    "terms" : [
        { "term" : String, "pos" : String, "definition" : String },
        { "term" : String, "pos" : String, "definition" : String },
        ...
    ]
}

Each vocabulary set has a name and a collection of terms, each having a single POS (part-of-speech type) and definition.

Write a function named longestWord that takes as a parameter an object called vocab using the above schema and returns the term with the longest length, breaking ties alphabetically.

For example, if the following vocabulary set is defined:

{
    "setname" : "SAT Set 1",
    "terms" : [
        { "term" : "abate", 
          "pos" : "verb", 
          "definition" : "lessen" },
        { "term" : "abnegation", 
          "pos" : "noun", 
          "definition" : "the denial of a doctrine or belief" },
        { "term" : "adept", 
          "pos" : "adjective", 
          "definition" : "skillful" },
        { "term" : "advocate", 
          "pos" : "noun", 
          "definition" : "a person who pleads for a person, cause, or idea" },
        { "term" : "adroit", 
          "pos" : "adjective", 
          "definition" : "skillful" },
        { "term" : "annex", 
          "pos" : "noun", 
          "definition" : "extension to a building" },
        { "term" : "annex", 
          "pos" : "verb", 
          "definition" : "to take away from" },
        { "term" : "antediluvian", 
          "pos" : "adjective", 
          "definition" : "very old" }
    ]
};

then the call longestWord(vocab) should return the string "antediluvian". If the terms array is empty, your function should return "".

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

You may otherwise assume all of the keys of each object in the "terms" 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.