Question
Question: build a query string * * Querying a web data API involves formatting a query string in a particular way. * As we know
Question: build a query string
*
* Querying a web data API involves formatting a query string in a particular way.
* As we know from week 1, a query string is a set of name=value pairs, that starts
* with the ? character, and each pair is separated by the & character, see:
* https://web222.ca/weeks/week01/#query-strings
*
* For example:
*
* ?q=dog&sort=ascending includes q=dog and sort=ascending
* ?_encoding=UTF8&node=18521080011 includes both _encoding=UTF8 and also node=18521080011
*
* Write a buildQueryString() function to build a query string based on arguments
* passed by the caller.
*
* The buildQueryString() function accepts the following arguments:
*
* - queryTerm: a search string, for example "butterfly" or "Horse-chestnut"
* - sortOrder: a string indicating sort order, with possible values of `ascending` or `descending`
* - count: a number from 10 to 200, indicating how many results to return per page
*
* Write an implementation of buildQueryString() that accepts arguments for all of the above
* parameters, validates and formats them (e.g., encode the query, count must be between 10
* and 200, etc), and returns a properly formatted query string.
*
* For example:
*
* buildQueryString('Monarch Butterfly', 'ascending', 25) would return the following query string:
*
* '?query_term=Monarch%20Butterfly&count=25
*
* NOTE: the default sort order is ascending, so it isn't included. However, if we were to
* specify 'descending', it would need to get added:
*
* buildQueryString('Monarch Butterfly', 'descending', 25) would return the following query string:
*
* '?query_term=Monarch%20Butterfly&count=25&descending
*
* NOTE: if any of the values passed to buildQueryString() are invalid, an Error should be thrown.
*
* NOTE: make sure you properly encode the query value, since query strings can't contain
* spaces or other special characters. HINT: use the encodeURIComponent() function
* to do this, see:
*
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
*
* @param {string} queryTerm the query to use.
* @param {string} sortOrder the sort order to use, must be one of `ascending` or `descending`
* @param {number} count the number of results per page, must be 10-200
* @returns {string} the properly formatted query string
******************************************************************************/
function buildQueryString(queryTerm, sortOrder, count) {
// Replace this comment with your code...
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started