Strings - Exercise 3
Question 1: Capitalize an ASCII Word Completed
Return the word with its first character uppercased and the rest lowercased. Return an empty string for empty input.
Expected output: "Javascript" (print this with console.log)
function capitalizeAsciiWord(word) {
// Write your solution here
}
console.log(capitalizeAsciiWord("jAVASCRIPT"));function capitalizeAsciiWord(word) {
return word ? word[0].toUpperCase() + word.slice(1).toLowerCase() : word;
}
console.log(capitalizeAsciiWord("jAVASCRIPT"));Question 2: Create Initials Completed
Return the uppercase first letters of two non-empty names with no separator.
Expected output: "AL" (print this with console.log)
function createNameInitials(first, last) {
// Write your solution here
}
console.log(createNameInitials("ada", "lovelace"));function createNameInitials(first, last) {
return first[0].toUpperCase() + last[0].toUpperCase();
}
console.log(createNameInitials("ada", "lovelace"));Question 3: Mask All but Four Digits Completed
Replace all but the last four characters of a code with asterisks. Codes shorter than four characters are left unchanged.
Expected output: "****5678" (print this with console.log)
function maskCodeExceptLastFour(code) {
// Write your solution here
}
console.log(maskCodeExceptLastFour("12345678"));function maskCodeExceptLastFour(code) {
return "*".repeat(Math.max(0, code.length - 4)) + code.slice(-4);
}
console.log(maskCodeExceptLastFour("12345678"));Question 4: Create a Slug from Words Completed
Trim the input, lowercase it, and replace each run of whitespace with one hyphen.
Expected output: "hello-world" (print this with console.log)
function createWhitespaceSlug(text) {
// Write your solution here
}
console.log(createWhitespaceSlug(" Hello World "));function createWhitespaceSlug(text) {
return text.trim().toLowerCase().replace(/\s+/g, "-");
}
console.log(createWhitespaceSlug(" Hello World "));Question 5: Normalize Internal Spaces Completed
Trim the string and replace each run of whitespace with a single ordinary space.
Expected output: "one two" (print this with console.log)
function normalizeTextWhitespace(text) {
// Write your solution here
}
console.log(normalizeTextWhitespace(" one two "));function normalizeTextWhitespace(text) {
return text.trim().replace(/\s+/g, " ");
}
console.log(normalizeTextWhitespace(" one two "));Question 6: Format an Extension Completed
Remove one leading dot from the extension if present, convert it to lowercase, and prefix it with a dot.
Expected output: ".txt" (print this with console.log)
function normalizeFileExtension(extension) {
// Write your solution here
}
console.log(normalizeFileExtension(".TXT"));function normalizeFileExtension(extension) {
return "." + (extension.startsWith(".") ? extension.slice(1) : extension).toLowerCase();
}
console.log(normalizeFileExtension(".TXT"));Question 7: Compare Words Ignoring Case Completed
Return whether the two supplied strings match after lowercasing them.
Expected output: true (print this with console.log)
function wordsEqualIgnoringCase(first, second) {
// Write your solution here
}
console.log(wordsEqualIgnoringCase("Hello", "HELLO"));function wordsEqualIgnoringCase(first, second) {
return first.toLowerCase() === second.toLowerCase();
}
console.log(wordsEqualIgnoringCase("Hello", "HELLO"));Question 8: Count Comma-Separated Fields Completed
Return the number of fields separated by commas, counting empty fields. An empty string has one field.
Expected output: 3 (print this with console.log)
function countCommaFields(text) {
// Write your solution here
}
console.log(countCommaFields("a,,b"));function countCommaFields(text) {
return text.split(",").length;
}
console.log(countCommaFields("a,,b"));Question 9: Create a Repeated Divider Completed
Return a string consisting of the supplied character repeated the non-negative integer count times.
Expected output: "-----" (print this with console.log)
function makeRepeatedDivider(character, count) {
// Write your solution here
}
console.log(makeRepeatedDivider("-", 5));function makeRepeatedDivider(character, count) {
return character.repeat(count);
}
console.log(makeRepeatedDivider("-", 5));Question 10: Format a Name Badge Completed
Trim the supplied name and wrap it in square brackets.
Expected output: "[Ada]" (print this with console.log)
function formatNameBadge(name) {
// Write your solution here
}
console.log(formatNameBadge(" Ada "));function formatNameBadge(name) {
return "[" + name.trim() + "]";
}
console.log(formatNameBadge(" Ada "));