Strings - Exercise 5

Question 1: Count Nonempty Words

Trim the text, then count words separated by one or more whitespace characters. Return zero for whitespace-only text.

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

javascript

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

console.log(countWhitespaceSeparatedWords("  one  two three "));

Question 2: Find the Longest Token Length

Return the length of the longest whitespace-separated token, or zero for an empty or whitespace-only string.

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

javascript

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

console.log(longestWhitespaceTokenLength("a longer word"));

Question 3: Replace an Exact Word

Split text on ordinary spaces and replace entries exactly equal to oldWord with newWord. Preserve the spaces when rejoining.

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

javascript

function replaceExactSpaceWord(text, oldWord, newWord) {
  // Write your solution here
}

console.log(replaceExactSpaceWord("cat dog cat", "cat", "fox"));

Question 4: List Unique ASCII Characters

Return a string of the input's distinct ASCII characters in the order they first appear.

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

javascript

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

console.log(uniqueAsciiCharacters("banana"));

Question 5: Count Case-Insensitive Matches

Return how many times the supplied single ASCII letter occurs in the text, ignoring case.

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

javascript

function countCaseInsensitiveLetter(text, letter) {
  // Write your solution here
}

console.log(countCaseInsensitiveLetter("BaNaNa", "a"));

Question 6: Check an ASCII Palindrome

Return true if the ASCII text reads the same forward and backward, ignoring letter case. Spaces and punctuation count.

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

javascript

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

console.log(isCaseInsensitiveAsciiPalindrome("Level"));

Question 7: Remove Consecutive Duplicate Characters

Return the ASCII string with each run of consecutive equal characters reduced to one.

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

javascript

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

console.log(collapseRepeatedAsciiChars("baalloon"));

Question 8: Read a Delimited Value

Return the part after the first colon in the input, or an empty string if there is no colon.

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

javascript

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

console.log(readAfterFirstColon("key:value:more"));

Question 9: Count Occurrences of a Substring

Return the number of non-overlapping occurrences of a nonempty substring in the text.

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

javascript

function countNonoverlappingSubstrings(text, part) {
  // Write your solution here
}

console.log(countNonoverlappingSubstrings("aaaaa", "aa"));

Question 10: Extract Text Between Markers

Return text between the first opening and following closing marker, or null when either is missing. Markers are nonempty strings.

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

javascript

function extractBetweenMarkers(text, open, close) {
  // Write your solution here
}

console.log(extractBetweenMarkers("a[b]c", "[", "]"));