All files / problems/namesBook index.ts

100% Statements 28/28
100% Branches 3/3
100% Functions 2/2
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 281x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 5x 5x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x
/**
 * Implement names book with array and linked list
 * Array will have 26 slots for each letter of the alphabet
 * In each slot will be linked list of names
 */
import LinkedList from "../../data-structure/linkedList";
 
const namesBook = () => {
  // name book is an array of 26 slots alphabetically a - z
  const book = new Array(26).fill(null);
 
  const insert = (name: string) => {
    // insert name into the book
    const firstLetter = name[0].toLowerCase();
    const index = firstLetter.charCodeAt(0) - 97;
    if (book[index] === null) {
      book[index] = LinkedList();
    }
    book[index].add(name);
  };
 
  return {
    insert,
    book,
  };
};
 
export default namesBook;