Strings - Exercise 4
Question 1: Remove a Query String Completed
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)
function removeUrlQuery(url) {
// Write your solution here
}
console.log(removeUrlQuery("/items?page=2"));function removeUrlQuery(url) {
const index = url.indexOf("?");
return index === -1 ? url : url.slice(0, index);
}
console.log(removeUrlQuery("/items?page=2"));Question 2: Read a URL Fragment Completed
Return the text after the first "#" or an empty string if no "#" is present.
Expected output: "intro" (print this with console.log)
function readUrlFragment(url) {
// Write your solution here
}
console.log(readUrlFragment("/guide#intro"));function readUrlFragment(url) {
const index = url.indexOf("#");
return index === -1 ? "" : url.slice(index + 1);
}
console.log(readUrlFragment("/guide#intro"));Question 3: Shorten a Label Completed
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)
function shortenTextLabel(label, max) {
// Write your solution here
}
console.log(shortenTextLabel("elephant", 5));function shortenTextLabel(label, max) {
return label.length > max ? label.slice(0, max - 3) + "..." : label;
}
console.log(shortenTextLabel("elephant", 5));Question 4: Convert a Comma List to Lines Completed
Replace each comma in the text with a newline character.
Expected output: "a\nb\nc" (print this with console.log)
function commasToNewlines(text) {
// Write your solution here
}
console.log(commasToNewlines("a,b,c"));function commasToNewlines(text) {
return text.replaceAll(",", "\n");
}
console.log(commasToNewlines("a,b,c"));Question 5: Remove a Trailing Slash Completed
Remove exactly one trailing slash when present; otherwise return the path unchanged.
Expected output: "/docs" (print this with console.log)
function removeOneTrailingSlash(path) {
// Write your solution here
}
console.log(removeOneTrailingSlash("/docs/"));function removeOneTrailingSlash(path) {
return path.endsWith("/") ? path.slice(0, -1) : path;
}
console.log(removeOneTrailingSlash("/docs/"));Question 6: Ensure a Leading Slash Completed
Add a leading slash only if the path does not already start with one.
Expected output: "/docs" (print this with console.log)
function ensureLeadingSlash(path) {
// Write your solution here
}
console.log(ensureLeadingSlash("docs"));function ensureLeadingSlash(path) {
return path.startsWith("/") ? path : "/" + path;
}
console.log(ensureLeadingSlash("docs"));Question 7: Read the First Line Completed
Return the text before the first newline, or the whole input when there is no newline.
Expected output: "one" (print this with console.log)
function readFirstTextLine(text) {
// Write your solution here
}
console.log(readFirstTextLine("one\ntwo"));function readFirstTextLine(text) {
return text.split("\n")[0];
}
console.log(readFirstTextLine("one\ntwo"));Question 8: Read the Final Path Segment Completed
Return the text after the final slash; when there is no slash, return the whole path.
Expected output: "intro" (print this with console.log)
function readFinalPathSegment(path) {
// Write your solution here
}
console.log(readFinalPathSegment("/docs/intro"));function readFinalPathSegment(path) {
return path.slice(path.lastIndexOf("/") + 1);
}
console.log(readFinalPathSegment("/docs/intro"));Question 9: Check Matching Brackets Completed
Return true if the string begins with "[" and ends with "]".
Expected output: true (print this with console.log)
function hasOuterSquareBrackets(text) {
// Write your solution here
}
console.log(hasOuterSquareBrackets("[item]"));function hasOuterSquareBrackets(text) {
return text.startsWith("[") && text.endsWith("]");
}
console.log(hasOuterSquareBrackets("[item]"));Question 10: Build an HTML-Like Tag Completed
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)
function wrapPlainTextInTag(tag, text) {
// Write your solution here
}
console.log(wrapPlainTextInTag("b", "Hello"));function wrapPlainTextInTag(tag, text) {
return "<" + tag + ">" + text + "</" + tag + ">";
}
console.log(wrapPlainTextInTag("b", "Hello"));