Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12520,6 +12520,37 @@ This question is really showcasing how JavaScript mixes array reduction with low
</p>
</details>

### 88.What is the output of the following snippet?
***javascript
console.log("A");

setTimeout(() => {
console.log("B");
}, 0);

Promise.resolve().then(() => {
console.log("C");
});

console.log("D");

***

- 1. A D B C
- 2. A D C B
- 3. A C D B
- 4. A B C D

<details><summary><b>Answer</b></summary>
<p>
### Answer : 2

The output is A D C B because JavaScript executes all synchronous code first, so “A” and “D” are printed immediately. After that, the event loop handles asynchronous tasks, where Promise.then() is placed in the microtask queue and setTimeout(...,0) goes to the macrotask queue. Since microtasks always execute before macrotasks—regardless of the timeout value—the promise callback prints “C” next, and finally the timeout callback prints “B”, resulting in the final order: A D C B.

</p>
</details>


**[⬆ Back to Top](#table-of-contents)**

## Disclaimer
Expand Down
28 changes: 28 additions & 0 deletions coding-exercise/deepclone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function deepClone(value, hash = new WeakMap()) {
// Handle primitives
if (value === null || typeof value !== "object") return value;

// Handle circular references
if (hash.has(value)) return hash.get(value);

// Handle Date
if (value instanceof Date) return new Date(value);

// Handle RegExp
if (value instanceof RegExp) return new RegExp(value);

// Handle Arrays or Objects
const clone = Array.isArray(value) ? [] : {};

// Store reference in WeakMap (for circular structures)
hash.set(value, clone);

// Recursively clone properties
for (let key in value) {
if (value.hasOwnProperty(key)) {
clone[key] = deepClone(value[key], hash);
}
}

return clone;
}