Strings - Exercise 1
Question 1: Extract a Product Prefix Completed
Return the first three characters of the product code. Codes shorter than three characters return in full.
Expected output: "ABC" (print this with console.log)
function productCodePrefix(code) {
// Write your solution here
}
console.log(productCodePrefix("ABC123"));function productCodePrefix(code) {
return code.slice(0, 3);
}
console.log(productCodePrefix("ABC123"));Question 2: Extract a File Suffix Completed
Return the final four characters of the supplied filename. Shorter inputs are returned in full.
Expected output: ".txt" (print this with console.log)
function filenameLastFour(name) {
// Write your solution here
}
console.log(filenameLastFour("report.txt"));function filenameLastFour(name) {
return name.slice(-4);
}
console.log(filenameLastFour("report.txt"));Question 3: Remove the First Character Completed
Return the supplied string without its first character.
Expected output: "ello" (print this with console.log)
function withoutFirstCharacter(text) {
// Write your solution here
}
console.log(withoutFirstCharacter("hello"));function withoutFirstCharacter(text) {
return text.slice(1);
}
console.log(withoutFirstCharacter("hello"));Question 4: Remove the Last Character Completed
Return the supplied string without its last character.
Expected output: "hell" (print this with console.log)
function withoutLastCharacter(text) {
// Write your solution here
}
console.log(withoutLastCharacter("hello"));function withoutLastCharacter(text) {
return text.slice(0, -1);
}
console.log(withoutLastCharacter("hello"));Question 5: Extract a Middle Segment Completed
Return the substring starting at start and ending before end. The indices are valid non-negative integers.
Expected output: "sc" (print this with console.log)
function extractMiddleSegment(text, start, end) {
// Write your solution here
}
console.log(extractMiddleSegment("javascript", 4, 6));function extractMiddleSegment(text, start, end) {
return text.slice(start, end);
}
console.log(extractMiddleSegment("javascript", 4, 6));Question 6: Find a Substring Position Completed
Return the index of the first occurrence of needle in text, or -1 if it is absent.
Expected output: 2 (print this with console.log)
function findSubstringPosition(text, needle) {
// Write your solution here
}
console.log(findSubstringPosition("banana", "na"));function findSubstringPosition(text, needle) {
return text.indexOf(needle);
}
console.log(findSubstringPosition("banana", "na"));Question 7: Find a Final Substring Position Completed
Return the index of the last occurrence of needle in text, or -1 if absent.
Expected output: 4 (print this with console.log)
function findFinalSubstringPosition(text, needle) {
// Write your solution here
}
console.log(findFinalSubstringPosition("banana", "na"));function findFinalSubstringPosition(text, needle) {
return text.lastIndexOf(needle);
}
console.log(findFinalSubstringPosition("banana", "na"));Question 8: Check a Prefix Completed
Return true if the text starts with the supplied prefix. Matching is case-sensitive.
Expected output: true (print this with console.log)
function textHasPrefix(text, prefix) {
// Write your solution here
}
console.log(textHasPrefix("javascript", "java"));function textHasPrefix(text, prefix) {
return text.startsWith(prefix);
}
console.log(textHasPrefix("javascript", "java"));Question 9: Check a Suffix Completed
Return true if the text ends with the supplied suffix. Matching is case-sensitive.
Expected output: true (print this with console.log)
function textHasSuffix(text, suffix) {
// Write your solution here
}
console.log(textHasSuffix("notes.txt", ".txt"));function textHasSuffix(text, suffix) {
return text.endsWith(suffix);
}
console.log(textHasSuffix("notes.txt", ".txt"));Question 10: Check for a Phrase Completed
Return true if the supplied phrase occurs in the text, using case-sensitive matching.
Expected output: true (print this with console.log)
function textContainsPhrase(text, phrase) {
// Write your solution here
}
console.log(textContainsPhrase("hello world", "world"));function textContainsPhrase(text, phrase) {
return text.includes(phrase);
}
console.log(textContainsPhrase("hello world", "world"));