Another day of starting a bit late. I didn’t actually wake up too late, but I did go back to sleep after Tommy left for work since I hadn’t rested well the night before. I kept waking up throughout the night, and I’m not sure why, which was quite frustrating. Eventually, I got up, showered, and went through my morning routine. I haven’t eaten anything yet, but I plan to eat during lunchtime.
The weather will be in the 70s today, and I’m thrilled that it’s finally warming up. I’m looking forward to working at my new desk. As Kel mentioned yesterday, my current desk sounds like a horror movie; it creaks with every movement I make, and even just typing causes it to whine. It’s amusing, really! I’m still contemplating how I want to decorate my new workspace. I tend to lean towards a minimalist approach, preferring to keep my desk uncluttered. However, it doesn’t always work out that way with the amount of stuff I have.
Around Christmas, Tommy asked me what I wanted for Christmas and I mentioned that I would like something from Sephora. He allowed me to use his card for the purchase. Yesterday, I visited the Sephora website and took advantage of a sale to get a few things I needed. I believed I had paid with PayPal, but when I checked my account, I noticed that no money had been deducted. Today, I went back to Sephora to review my payment options and realized that the only payment method linked was Tommy’s card. I inadvertently charged my purchase to his card! I texted Tommy and immediately transferred the money to his account. Additionally, I deleted his card from my Sephora account to prevent this from happening again. I can’t believe I didn’t pay closer attention to my payment method.
In our coding exercise, we will write a function that returns the first non-repeating character in a given string. Although my mind feels a bit like mush today, let’s give this a try. The `console.log` examples look like this:
firstUniqueChar("swiss"); // Output: "w" firstUniqueChar("racecar"); // Output: "e"
So the skeleton of the code probably looks like this:
function firstUniqueChar(str) { // Code here }
To break it down, we want to:
- Loop through the string and count how many times each character appears.
- Store those counts in a map (object in JavaScript).
- Loop through the string again and return the first character whose count is 1.
Ok, we need a loop.
function firstUniqueChar(str) { const charCount = {}; // First pass: Count each character for (let char of str) { if (charCount[char]) { charCount[char]++; } else { charCount[char] = 1; } } }
const charCount = {};
- We’re creating an empty object (like a dictionary) to store character counts.
- Each key will be a character from the string, and each value will be how many times it appears.
for (let char of str) {
- This loop goes through the string one character at a time.
- For example, if str = “hello”, the loop will go through ‘h’, ‘e’, ‘l’, ‘l’, ‘o’.
if (charCount[char]) {
- This checks if we’ve already seen this character before.
- charCount[char] will be undefined the first time we see a character.
- If it does exist, that means we already added it and we need to increment the count.
charCount[char]++;
- If the character is already in the object, add 1 to its current count.
charCount[char] = 1;
- If the character isn’t in the object yet, this sets its count to 1 — meaning this is the first time we’ve seen it.
Let’s say str = “hello”
The loop goes like this:
- ‘h’ → not in charCount → set to 1
- ‘e’ → not in charCount → set to 1
- ‘l’ → not in charCount → set to 1
- ‘l’ → already in charCount → increment to 2
- ‘o’ → not in charCount → set to 1
Now we need second loop:
function firstUniqueChar(str) { const charCount = {}; // First pass: Count each character for (let char of str) { if (charCount[char]) { charCount[char]++; } else { charCount[char] = 1; } } // Second pass: Find the first unique character for (let char of str) { if (charCount[char] === 1) { return char; } } // If no unique character found return null; }
for (let char of str) {
- We loop through each character in the original string again.
- This ensures we preserve the original order of characters — because we want the first unique one.
if (charCount[char] === 1) {
- This checks if the current character appears only once in the string.
- charCount is the object we filled earlier, where each key is a character and its value is how many times it appears.
return char;
- As soon as we find the first character with a count of 1, we immediately return it.
- That ends the function right there.
If no unique character is found:
return null;
- If we go through the whole string and don’t find any characters with a count of 1, we return null.
- This tells us: there is no non-repeating character.
Example:
For str = “aabbcdeff”
charCount = { a: 2, b: 2, c: 1, d: 1, e: 1, f: 2 }
We loop through “aabbcdeff”
- a → count is 2 ❌
- a → ❌
- b → ❌
- b → ❌
- c → count is 1 ✅ → we return “c” //the first non-repeating character in a given string
It took me a little over a few hours to figure that out. As I mentioned, my mind feels a bit foggy, and I’m having difficulty concentrating. The next exercise is to write a function that mimics Function.prototype.bind. I’m not entirely clear on what that entails. The bind method creates a new function with a specific ‘this’ value and, optionally, preset arguments. However, that doesn’t clarify much for me. I’ll need to do a bit of research on this.





