Strings - Exercise 3

Question 1: Capitalize an ASCII Word

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)

javascript

function capitalizeAsciiWord(word) {
  // Write your solution here
}

console.log(capitalizeAsciiWord("jAVASCRIPT"));

Question 2: Create Initials

Return the uppercase first letters of two non-empty names with no separator.

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

javascript

function createNameInitials(first, last) {
  // Write your solution here
}

console.log(createNameInitials("ada", "lovelace"));

Question 3: Mask All but Four Digits

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)

javascript

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

console.log(maskCodeExceptLastFour("12345678"));

Question 4: Create a Slug from Words

Trim the input, lowercase it, and replace each run of whitespace with one hyphen.

Expected output: "hello-world" (print this with console.log)

javascript

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

console.log(createWhitespaceSlug("  Hello   World  "));

Question 5: Normalize Internal Spaces

Trim the string and replace each run of whitespace with a single ordinary space.

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

javascript

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

console.log(normalizeTextWhitespace("  one   two  "));

Question 6: Format an Extension

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)

javascript

function normalizeFileExtension(extension) {
  // Write your solution here
}

console.log(normalizeFileExtension(".TXT"));

Question 7: Compare Words Ignoring Case

Return whether the two supplied strings match after lowercasing them.

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

javascript

function wordsEqualIgnoringCase(first, second) {
  // Write your solution here
}

console.log(wordsEqualIgnoringCase("Hello", "HELLO"));

Question 8: Count Comma-Separated Fields

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)

javascript

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

console.log(countCommaFields("a,,b"));

Question 9: Create a Repeated Divider

Return a string consisting of the supplied character repeated the non-negative integer count times.

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

javascript

function makeRepeatedDivider(character, count) {
  // Write your solution here
}

console.log(makeRepeatedDivider("-", 5));

Question 10: Format a Name Badge

Trim the supplied name and wrap it in square brackets.

Expected output: "[Ada]" (print this with console.log)

javascript

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

console.log(formatNameBadge("  Ada  "));