Parse a URL and report its host.
Acceptance criteria
- 1.#out should show example.com.
JavaScript Tutorial · JavaScript Storage & BOM
These 10 tasks go with the JavaScript location Object chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “location describes the current URL and can be used to navigate to another page.” .
1.Read the hostnameexercise · easy · 10 min
Parse a URL and report its host.
Passes when: #out should show example.com.
2.Read the pathexercise · easy · 10 min · practice plan
Parse a URL and report its path.
Passes when: #out should show /products.
3.Read a query parameterexercise · easy · 10 min · practice plan
Parse the query string and read the id.
Passes when: #out should show 42.
4.Handle a missing parameterexercise · easy · 10 min · practice plan
Report what a missing parameter gives back.
Passes when: #out should show not present.
5.Build a query stringexercise · medium · 10 min · practice plan
Build a query string from two parameters.
Passes when: #out should show q=javascript&page=2.
6.Fix search read without URLSearchParamsbug fix · easy · 8 min
The raw search string still has the question mark and the key attached.
Passes when: #out should show just the id.
7.Fix pathname read as hostnamebug fix · easy · 8 min · practice plan
hostname and pathname give different parts of the URL.
Passes when: #out should show /products.
8.Fix the missing parameter uncheckedbug fix · easy · 8 min · practice plan
A missing parameter gives null, which must be checked.
Passes when: #out should show not present.
9.Fix the query string built by handbug fix · easy · 8 min · practice plan
Joining strings misses the escaping a real query string needs.
Passes when: #out should show q=hello+world.
10.Fix the parameter never setbug fix · easy · 8 min · practice plan
The value must be set before toString includes it.
Passes when: #out should show q=javascript.
Between them these tasks cover URL, URLSearchParams and Parsing.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.