In this module, you'll refresh your understanding of objects and learn techniques for letter counting in JavaScript. This foundational knowledge will be crucial for your technical preparation.
Objects are fundamental data structures in JavaScript used to store key-value pairs. In this video, you'll review the core concepts of object creation, manipulation, and traversal.
// Creating an object const person = { name: 'John', age: 30, skills: ['JavaScript', 'React', 'Node.js'] }; // Accessing properties - dot notation console.log(person.name); // 'John' // Accessing properties - bracket notation console.log(person['age']); // 30 // Adding new properties person.location = 'San Francisco'; // Modifying properties person.age = 31; // Iterating through properties for (const key in person) { console.log(`${key}: ${person[key]}`); }
Related Practice: LeetCode: Two Sum - A classic problem that tests your ability to use objects as lookup tables.
In this section, you'll learn how to approach the letter counting problem by first understanding the requirements and constraints.
Related Practice: LeetCode: Valid Anagram - Tests your ability to count and compare character frequencies.
This video covers planning your approach to the letter count problem, creating pseudocode, and designing your algorithm before writing any actual code.
/* 1. Initialize an empty object to store letter counts 2. Convert the input string to lowercase (optional depending on requirements) 3. Loop through each character in the string a. If the character is a letter, increment its count in the object b. If the character doesn't exist in the object yet, initialize it with a count of 1 4. Return the resulting object with letter frequencies */
In this video, you'll implement the letter count algorithm with actual code, translating your plan into a working solution.
function letterCount(str) { // Create an empty object to hold letter counts const counts = {}; // Convert the string to lowercase and remove non-alphabetic characters const letters = str.toLowerCase().replace(/[^a-z]/g, ''); // Count occurrences of each letter for (const char of letters) { if (counts[char]) { counts[char]++; } else { counts[char] = 1; } } return counts; } // Example usage console.log(letterCount("Hello, World!")); // Output: { h: 1, e: 1, l: 3, o: 2, w: 1, r: 1, d: 1 }
Related Practice: LeetCode: First Unique Character in a String - Uses character counting to find the first non-repeating character.
This final video in the letter count series focuses on debugging your solution, testing for edge cases, and ensuring your code is robust.
function letterCount(str) { // Handle edge case: empty or non-string input if (!str || typeof str !== 'string') { return {}; } const counts = {}; const letters = str.toLowerCase().replace(/[^a-z]/g, ''); // Handle edge case: no letters in the string if (letters.length === 0) { return {}; } for (const char of letters) { counts[char] = (counts[char] || 0) + 1; // Simplified increment using logical OR } return counts; } // Test edge cases console.log(letterCount("")); // {} console.log(letterCount("123!@#")); // {} console.log(letterCount("Hello!")); // { h: 1, e: 1, l: 2, o: 1 }
Related Practice: LeetCode: Longest Substring Without Repeating Characters - A more advanced problem that builds on character counting techniques.
Complete the Module 1 Guided Project to practice your object manipulation and letter counting skills. This project will help you apply the concepts from this module to solve a real-world problem.
Instead of using CodeSignal Arcade which is no longer available, we recommend the following LeetCode collections that follow the same principles and provide great alternatives for interview preparation: