Strings - Exercise 4

Question 1: Remove a Query String

Return the part of the URL before the first "?", or the whole URL if it has no question mark.

Expected output: "/items" (print this with console.log)

javascript

function removeUrlQuery(url) {
  // Write your solution here
}

console.log(removeUrlQuery("/items?page=2"));

Question 2: Read a URL Fragment

Return the text after the first "#" or an empty string if no "#" is present.

Expected output: "intro" (print this with console.log)

javascript

function readUrlFragment(url) {
  // Write your solution here
}

console.log(readUrlFragment("/guide#intro"));

Question 3: Shorten a Label

If the label exceeds the maximum length, return its first max - 3 characters followed by "..."; otherwise return it unchanged. The maximum is at least 3.

Expected output: "el..." (print this with console.log)

javascript

function shortenTextLabel(label, max) {
  // Write your solution here
}

console.log(shortenTextLabel("elephant", 5));

Question 4: Convert a Comma List to Lines

Replace each comma in the text with a newline character.

Expected output: "a\nb\nc" (print this with console.log)

javascript

function commasToNewlines(text) {
  // Write your solution here
}

console.log(commasToNewlines("a,b,c"));

Question 5: Remove a Trailing Slash

Remove exactly one trailing slash when present; otherwise return the path unchanged.

Expected output: "/docs" (print this with console.log)

javascript

function removeOneTrailingSlash(path) {
  // Write your solution here
}

console.log(removeOneTrailingSlash("/docs/"));

Question 6: Ensure a Leading Slash

Add a leading slash only if the path does not already start with one.

Expected output: "/docs" (print this with console.log)

javascript

function ensureLeadingSlash(path) {
  // Write your solution here
}

console.log(ensureLeadingSlash("docs"));

Question 7: Read the First Line

Return the text before the first newline, or the whole input when there is no newline.

Expected output: "one" (print this with console.log)

javascript

function readFirstTextLine(text) {
  // Write your solution here
}

console.log(readFirstTextLine("one\ntwo"));

Question 8: Read the Final Path Segment

Return the text after the final slash; when there is no slash, return the whole path.

Expected output: "intro" (print this with console.log)

javascript

function readFinalPathSegment(path) {
  // Write your solution here
}

console.log(readFinalPathSegment("/docs/intro"));

Question 9: Check Matching Brackets

Return true if the string begins with "[" and ends with "]".

Expected output: true (print this with console.log)

javascript

function hasOuterSquareBrackets(text) {
  // Write your solution here
}

console.log(hasOuterSquareBrackets("[item]"));

Question 10: Build an HTML-Like Tag

Return the supplied text surrounded by an opening and closing tag using the provided tag name. Inputs contain plain text, not HTML.

Expected output: "<b>Hello</b>" (print this with console.log)

javascript

function wrapPlainTextInTag(tag, text) {
  // Write your solution here
}

console.log(wrapPlainTextInTag("b", "Hello"));