Strings - Exercise 2

Question 1: Replace the First Separator

Return the text with its first underscore replaced by a hyphen.

Expected output: "first-second_third" (print this with console.log)

javascript

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

console.log(replaceFirstUnderscore("first_second_third"));

Question 2: Replace Every Separator

Return the text with all underscores replaced by hyphens.

Expected output: "first-second-third" (print this with console.log)

javascript

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

console.log(replaceAllUnderscores("first_second_third"));

Question 3: Split a Comma List

Split the input at commas and return all pieces, including empty pieces.

Expected output: ["red","green","blue"] (print this with console.log)

javascript

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

console.log(splitCommaList("red,green,blue"));

Question 4: Extract the Username

Return the part of an email string before the first "@". The input always contains "@".

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

javascript

function emailUsernamePart(email) {
  // Write your solution here
}

console.log(emailUsernamePart("ana@example.com"));

Question 5: Extract an Email Domain

Return the portion after the first "@" in an email string. The input always contains "@".

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

javascript

function emailDomainPart(email) {
  // Write your solution here
}

console.log(emailDomainPart("ana@example.com"));

Question 6: Create a Trimmed Lowercase Tag

Trim outer whitespace and return the remaining text in lowercase.

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

javascript

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

console.log(normalizedTextTag("  HeLLo  "));

Question 7: Create a Four-Digit Identifier

Convert the non-negative integer to text and pad it on the left with zeroes to at least four characters.

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

javascript

function fourDigitIdentifier(number) {
  // Write your solution here
}

console.log(fourDigitIdentifier(7));

Question 8: Pad a Table Label

Pad the end of the label with periods until it is at least the supplied length.

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

javascript

function padTableLabel(label, length) {
  // Write your solution here
}

console.log(padTableLabel("cat", 5));

Question 9: Remove a Known Prefix

Remove the supplied prefix only when the text starts with it; otherwise return the text unchanged.

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

javascript

function removeKnownTextPrefix(text, prefix) {
  // Write your solution here
}

console.log(removeKnownTextPrefix("unhappy", "un"));

Question 10: Remove a Known Suffix

Remove a nonempty supplied suffix only when the text ends with it; otherwise return the text unchanged.

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

javascript

function removeKnownTextSuffix(text, suffix) {
  // Write your solution here
}

console.log(removeKnownTextSuffix("report.txt", ".txt"));