Build a Query String from Typed Params
Write the code, see it run, pass the checks.
The problem
- medium
- Code
- Pro
Write toQuery(params): build a URL query string like ?page=2&q=red%20shoes from an object whose values are strings, numbers, booleans or undefined. Leave out undefined values, keep the object's key order, encode keys and values with encodeURIComponent, and return an empty string when nothing is left. Its type must reject nested objects and arrays.
Your code must pass
- Builds the query in key order
- Encodes keys and values
- Leaves out undefined, and returns an empty string for nothing
- Has no type errors
- Accepts strings, numbers, booleans and undefined
- Rejects nested objects and arrays