Strings - Exercise 1

Question 1: Extract a Product Prefix

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)

javascript

function productCodePrefix(code) {
  // Write your solution here
}

console.log(productCodePrefix("ABC123"));

Question 2: Extract a File Suffix

Return the final four characters of the supplied filename. Shorter inputs are returned in full.

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

javascript

function filenameLastFour(name) {
  // Write your solution here
}

console.log(filenameLastFour("report.txt"));

Question 3: Remove the First Character

Return the supplied string without its first character.

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

javascript

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

console.log(withoutFirstCharacter("hello"));

Question 4: Remove the Last Character

Return the supplied string without its last character.

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

javascript

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

console.log(withoutLastCharacter("hello"));

Question 5: Extract a Middle Segment

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)

javascript

function extractMiddleSegment(text, start, end) {
  // Write your solution here
}

console.log(extractMiddleSegment("javascript", 4, 6));

Question 6: Find a Substring Position

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)

javascript

function findSubstringPosition(text, needle) {
  // Write your solution here
}

console.log(findSubstringPosition("banana", "na"));

Question 7: Find a Final Substring Position

Return the index of the last occurrence of needle in text, or -1 if absent.

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

javascript

function findFinalSubstringPosition(text, needle) {
  // Write your solution here
}

console.log(findFinalSubstringPosition("banana", "na"));

Question 8: Check a Prefix

Return true if the text starts with the supplied prefix. Matching is case-sensitive.

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

javascript

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

console.log(textHasPrefix("javascript", "java"));

Question 9: Check a Suffix

Return true if the text ends with the supplied suffix. Matching is case-sensitive.

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

javascript

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

console.log(textHasSuffix("notes.txt", ".txt"));

Question 10: Check for a Phrase

Return true if the supplied phrase occurs in the text, using case-sensitive matching.

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

javascript

function textContainsPhrase(text, phrase) {
  // Write your solution here
}

console.log(textContainsPhrase("hello world", "world"));