Strings - Exercise 2
Question 1: Replace the First Separator Completed
Return the text with its first underscore replaced by a hyphen.
Expected output: "first-second_third" (print this with console.log)
function replaceFirstUnderscore(text) {
// Write your solution here
}
console.log(replaceFirstUnderscore("first_second_third"));function replaceFirstUnderscore(text) {
return text.replace("_", "-");
}
console.log(replaceFirstUnderscore("first_second_third"));Question 2: Replace Every Separator Completed
Return the text with all underscores replaced by hyphens.
Expected output: "first-second-third" (print this with console.log)
function replaceAllUnderscores(text) {
// Write your solution here
}
console.log(replaceAllUnderscores("first_second_third"));function replaceAllUnderscores(text) {
return text.replaceAll("_", "-");
}
console.log(replaceAllUnderscores("first_second_third"));Question 3: Split a Comma List Completed
Split the input at commas and return all pieces, including empty pieces.
Expected output: ["red","green","blue"] (print this with console.log)
function splitCommaList(text) {
// Write your solution here
}
console.log(splitCommaList("red,green,blue"));function splitCommaList(text) {
return text.split(",");
}
console.log(splitCommaList("red,green,blue"));Question 4: Extract the Username Completed
Return the part of an email string before the first "@". The input always contains "@".
Expected output: "ana" (print this with console.log)
function emailUsernamePart(email) {
// Write your solution here
}
console.log(emailUsernamePart("ana@example.com"));function emailUsernamePart(email) {
return email.slice(0, email.indexOf("@"));
}
console.log(emailUsernamePart("ana@example.com"));Question 5: Extract an Email Domain Completed
Return the portion after the first "@" in an email string. The input always contains "@".
Expected output: "example.com" (print this with console.log)
function emailDomainPart(email) {
// Write your solution here
}
console.log(emailDomainPart("ana@example.com"));function emailDomainPart(email) {
return email.slice(email.indexOf("@") + 1);
}
console.log(emailDomainPart("ana@example.com"));Question 6: Create a Trimmed Lowercase Tag Completed
Trim outer whitespace and return the remaining text in lowercase.
Expected output: "hello" (print this with console.log)
function normalizedTextTag(text) {
// Write your solution here
}
console.log(normalizedTextTag(" HeLLo "));function normalizedTextTag(text) {
return text.trim().toLowerCase();
}
console.log(normalizedTextTag(" HeLLo "));Question 7: Create a Four-Digit Identifier Completed
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)
function fourDigitIdentifier(number) {
// Write your solution here
}
console.log(fourDigitIdentifier(7));function fourDigitIdentifier(number) {
return String(number).padStart(4, "0");
}
console.log(fourDigitIdentifier(7));Question 8: Pad a Table Label Completed
Pad the end of the label with periods until it is at least the supplied length.
Expected output: "cat.." (print this with console.log)
function padTableLabel(label, length) {
// Write your solution here
}
console.log(padTableLabel("cat", 5));function padTableLabel(label, length) {
return label.padEnd(length, ".");
}
console.log(padTableLabel("cat", 5));Question 9: Remove a Known Prefix Completed
Remove the supplied prefix only when the text starts with it; otherwise return the text unchanged.
Expected output: "happy" (print this with console.log)
function removeKnownTextPrefix(text, prefix) {
// Write your solution here
}
console.log(removeKnownTextPrefix("unhappy", "un"));function removeKnownTextPrefix(text, prefix) {
return text.startsWith(prefix) ? text.slice(prefix.length) : text;
}
console.log(removeKnownTextPrefix("unhappy", "un"));Question 10: Remove a Known Suffix Completed
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)
function removeKnownTextSuffix(text, suffix) {
// Write your solution here
}
console.log(removeKnownTextSuffix("report.txt", ".txt"));function removeKnownTextSuffix(text, suffix) {
return text.endsWith(suffix) ? text.slice(0, -suffix.length) : text;
}
console.log(removeKnownTextSuffix("report.txt", ".txt"));