I will begin with my coding notes and then share my thoughts about the day at the end, as I’m not quite sure what I want to write about yet. However, when it comes to coding, I feel kind of confident in this. I believe I have it figured out. Here’s the code:
function deepClone(obj) { if (obj === null || typeof obj !== 'object') { return obj; // Return the value if it's not an object } if (Array.isArray(obj)) { return obj.map(item => deepClone(item)); // Recursively clone arrays } let clonedObj = {}; // Create a new object for (let key in obj) { if (obj.hasOwnProperty(key)) { clonedObj[key] = deepClone(obj[key]); // Recursively clone each property } } return clonedObj; }
I explained the first part of the code yesterday. The concluding section of the code can be broken down as follows:
1️⃣ let clonedObj = {};
- A new empty object (clonedObj) is created.
- This will hold all the properties of the original object but as a new independent copy.
2️⃣ for (let key in obj) { … }
- This loops over all properties of obj.
- key represents each property name (e.g., “name”, “age”, “address”, etc.).
- The loop iterates over both direct and inherited properties, so we need to filter it.
3️⃣ if (obj.hasOwnProperty(key)) { … }
- .hasOwnProperty(key) ensures that we only copy the object’s own properties, not ones inherited from the prototype chain.
- Without this check, the function could mistakenly copy properties from Object.prototype.
4️⃣ clonedObj[key] = deepClone(obj[key]);
- This recursively calls deepClone(obj[key]) to make a deep copy of each property.
- If obj[key] is a nested object or array, the function calls itself to copy it properly.
- If obj[key] is a primitive value (like a string, number, or boolean), it just returns the value.
5️⃣ return clonedObj;
- After all properties have been cloned, the new object (clonedObj) is returned.
Now I need to write a throttle function. A throttle function is a technique in JavaScript that limits how often a function can be called over time. It ensures that a function executes at most once every specified time interval, even if it is triggered multiple times. I think I’ve done one like this before. The console.log comes with a hint this time:
const throttledLog = throttle(() => console.log("Throttled!"), 2000); throttledLog(); // Runs immediately throttledLog(); // Ignored if within 2 seconds Hint Use a timestamp to track when func was last executed.
We can start with:
function throttle(func, limit) { // Code goes here }
- func: The function we want to throttle (i.e., limit how often it runs).
- limit: The minimum time (in milliseconds) that must pass before func can run again.
It’s going to work like this:
- When the throttled function is called, it checks the time since the last execution.
- If limit milliseconds have passed, it executes func immediately.
- If limit milliseconds have NOT passed, it ignores the call.
This took some hours, but I think I have one that works:
function throttle(func, limit) { let lastCall = 0; // Store the last execution timestamp return function (...args) { const now = Date.now(); // Get the current timestamp if (now - lastCall >= limit) { lastCall = now; // Update last execution time func.apply(this, args); // Execute the function } }; }
To break this down:
1️⃣ let lastCall = 0;
- Purpose: This variable stores the last time func was executed.
- Why? We use it to track when the function was last called so that we can enforce the time limit (limit).
2️⃣ return function (…args) {
- This returns a new function that wraps func.
- The new function will enforce the throttling behavior.
- …args (spread operator) collects all arguments passed to the function.
3️⃣ const now = Date.now();
- Date.now() returns the current timestamp (in milliseconds).
- This tells us when the function is being called.
4️⃣ if (now – lastCall >= limit) {
- Checks if enough time (limit milliseconds) has passed since func was last executed.
- now – lastCall calculates the time difference between now and the last execution.
Two Possible Scenarios:
If now – lastCall >= limit
- Enough time has passed, so execute func.
If now – lastCall < limit
- Not enough time has passed, so ignore the call.
5️⃣ lastCall = now;
- Updates lastCall to the current time because func is being executed.
- This ensures the next call will be throttled correctly.
6️⃣ func.apply(this, args);
Calls the original function (func) with:
- this: The current execution context.
- args: The arguments passed to the throttled function.
Why use apply?
- It ensures that func receives all arguments properly.
- It preserves the this context when calling func.
Tonight, we’re fending for ourselves since Tommy has band practice, so we don’t have any dinner plans. I’m considering making the fried rice and chicken we had last night. I might also dive into playing Final Fantasy or watch some YouTube videos; I haven’t quite decided yet. Right now, I’m going to look for something to eat and enjoy some matcha tea. Today has mostly been spent coding and working through various functions.
Tomorrow, Tommy has a dentist appointment followed by therapy. We might plan to work out in the morning, but it seems like we don’t have many activities scheduled for the week. On Saturday, snow is in the forecast, and I’m hoping to have Chili Colorado for dinner. For now, I’m off to prepare my dinner.





