Strings - Exercise 5
Question 1: Count Nonempty Words Completed
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)
function countWhitespaceSeparatedWords(text) {
// Write your solution here
}
console.log(countWhitespaceSeparatedWords(" one two three "));function countWhitespaceSeparatedWords(text) {
const trimmed = text.trim();
return trimmed ? trimmed.split(/\s+/).length : 0;
}
console.log(countWhitespaceSeparatedWords(" one two three "));Question 2: Find the Longest Token Length Completed
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)
function longestWhitespaceTokenLength(text) {
// Write your solution here
}
console.log(longestWhitespaceTokenLength("a longer word"));function longestWhitespaceTokenLength(text) {
const trimmed = text.trim();
if (!trimmed) return 0;
return Math.max(...trimmed.split(/\s+/).map((word) => word.length));
}
console.log(longestWhitespaceTokenLength("a longer word"));Question 3: Replace an Exact Word Completed
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)
function replaceExactSpaceWord(text, oldWord, newWord) {
// Write your solution here
}
console.log(replaceExactSpaceWord("cat dog cat", "cat", "fox"));function replaceExactSpaceWord(text, oldWord, newWord) {
return text.split(" ").map((word) => word === oldWord ? newWord : word).join(" ");
}
console.log(replaceExactSpaceWord("cat dog cat", "cat", "fox"));Question 4: List Unique ASCII Characters Completed
Return a string of the input's distinct ASCII characters in the order they first appear.
Expected output: "ban" (print this with console.log)
function uniqueAsciiCharacters(text) {
// Write your solution here
}
console.log(uniqueAsciiCharacters("banana"));function uniqueAsciiCharacters(text) {
let result = "";
for (const char of text) if (!result.includes(char)) result += char;
return result;
}
console.log(uniqueAsciiCharacters("banana"));Question 5: Count Case-Insensitive Matches Completed
Return how many times the supplied single ASCII letter occurs in the text, ignoring case.
Expected output: 3 (print this with console.log)
function countCaseInsensitiveLetter(text, letter) {
// Write your solution here
}
console.log(countCaseInsensitiveLetter("BaNaNa", "a"));function countCaseInsensitiveLetter(text, letter) {
let count = 0;
for (const char of text.toLowerCase()) if (char === letter.toLowerCase()) count++;
return count;
}
console.log(countCaseInsensitiveLetter("BaNaNa", "a"));Question 6: Check an ASCII Palindrome Completed
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)
function isCaseInsensitiveAsciiPalindrome(text) {
// Write your solution here
}
console.log(isCaseInsensitiveAsciiPalindrome("Level"));function isCaseInsensitiveAsciiPalindrome(text) {
const normalized = text.toLowerCase();
return normalized === normalized.split("").reverse().join("");
}
console.log(isCaseInsensitiveAsciiPalindrome("Level"));Question 7: Remove Consecutive Duplicate Characters Completed
Return the ASCII string with each run of consecutive equal characters reduced to one.
Expected output: "balon" (print this with console.log)
function collapseRepeatedAsciiChars(text) {
// Write your solution here
}
console.log(collapseRepeatedAsciiChars("baalloon"));function collapseRepeatedAsciiChars(text) {
let result = "";
for (const char of text) if (char !== result[result.length - 1]) result += char;
return result;
}
console.log(collapseRepeatedAsciiChars("baalloon"));Question 8: Read a Delimited Value Completed
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)
function readAfterFirstColon(text) {
// Write your solution here
}
console.log(readAfterFirstColon("key:value:more"));function readAfterFirstColon(text) {
const index = text.indexOf(":");
return index < 0 ? "" : text.slice(index + 1);
}
console.log(readAfterFirstColon("key:value:more"));Question 9: Count Occurrences of a Substring Completed
Return the number of non-overlapping occurrences of a nonempty substring in the text.
Expected output: 2 (print this with console.log)
function countNonoverlappingSubstrings(text, part) {
// Write your solution here
}
console.log(countNonoverlappingSubstrings("aaaaa", "aa"));function countNonoverlappingSubstrings(text, part) {
let count = 0;
let index = 0;
while ((index = text.indexOf(part, index)) !== -1) { count++; index += part.length; }
return count;
}
console.log(countNonoverlappingSubstrings("aaaaa", "aa"));Question 10: Extract Text Between Markers Completed
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)
function extractBetweenMarkers(text, open, close) {
// Write your solution here
}
console.log(extractBetweenMarkers("a[b]c", "[", "]"));function extractBetweenMarkers(text, open, close) {
const start = text.indexOf(open);
if (start === -1) return null;
const end = text.indexOf(close, start + open.length);
return end === -1 ? null : text.slice(start + open.length, end);
}
console.log(extractBetweenMarkers("a[b]c", "[", "]"));