← Back to challenges

Decompose URL

JavaScriptHardloopsconditionsobjectsregex

Instructions

Create a function named that takes a string (URL) as input. Decompose the string and return an object with the following properties:

  • protocol: This is the url protocol (e.g. "http").
  • ipAdress: Should contain IP address (if valid) of URL or null.
  • subDomain: Should contain subDomain of URL (e.g. "www" or null).
  • domainName: Should contain domain without subDomain (e.g. "google.com" or null).
  • folderTree: Should contain an array of folders (e.g. "www.google.com/test/image/index.html"["test", "image"] or null)
  • targetFile: Should return targeted file.
  • argumentsFile: Should return arguments of targetedFile.

Examples

decomposeUrl("https://www.google.com/search/test.js?ok=1") ➞ {
  protocol: "https",
  ipAdress: null,
  subDomain: "www",
  domainName: "google.com",
  folderTree: (1) […],
  targetFile: "test.js",
  argumentsFile: "?ok=1"
}

decomposeUrl("https://innokodakademija.com/new/challenge") ➞ {
  protocol: "https",
  ipAdress: null,
  subDomain: null,
  domainName: "innokodakademija.com",
  folderTree: (1) […],
  targetFile: "challenge",
  argumentsFile: null
}

decomposeUrl("https://innokodakademija.com/new/challenge/test.html") ➞ {
  protocol: "https",
  ipAdress: null,
  subDomain: null,
  domainName: "innokodakademija.com",
  folderTree: (2) […],
  targetFile: "test.html",
  argumentsFile: null
}

Notes

N/A

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.