I need to have a URL Parser written in JavaScript, that will work as described below:
Assuming you have this URL http://www.somedomain.com/a/b/c
Then the function parseURL(n) will work as follows:
var paramA = parseURL(1) // Sets paramA to "a"
var paramB = parseURL(2) // Sets paramB to "b"
var paramC = parseURL(3) // Sets paramC to "c"
var paramD = parseURL(4) // Sets paramD to null (because the path does not ripple down to d)
also note that if I pass this URL http://www.somedomain.com/a/b/c/ (note that I added a backslash at the end of the URL) then the function will still work the same way.
This function should work on any URL of any length, so it should work on a longer URL as this one: http://www.somedomain.com/a/b/c/d/e/. For this URL. we can go up to parseURL(5) which should return “e”, but parseURL(6) or parseURL(7) will both return null.
Thanks.
As pointed by VisioN, to deal with ending
/, this version would be better :