Skip to content

Commit 99b0582

Browse files
authored
Added Question in dom.mdx (#71)
* Update dom.mdx Add Questions in dom.mdx * Update faq_js.mdx Added faq on javascript * Update faq_js.mdx * Update primitives.mdx
1 parent 3de5e8d commit 99b0582

3 files changed

Lines changed: 165 additions & 1 deletion

File tree

web/src/pages/challenges/dom.mdx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,61 @@ function serializeDOM(root) {
420420
- https://developer.mozilla.org/en-US/docs/Web/API/Element/attributes
421421

422422
---
423+
### 15. What is event delegation in the DOM and how can it be implemented in JavaScript?
424+
- Event delegation allows you to attach a single event listener to a parent element to manage events from its child elements, leveraging event bubbling. This improves performance and simplifies handling dynamically added elements.
425+
426+
Here’s an example of how event delegation works:
427+
```js copy
428+
document.querySelector('#parent').addEventListener('click', function(event) {
429+
if (event.target && event.target.matches('button')) {
430+
console.log('Button clicked:', event.target.textContent);
431+
}
432+
});
433+
```
434+
435+
This approach optimizes performance and reduces memory usage, as fewer event listeners are attached to individual elements.
436+
437+
**References**
438+
439+
- https://developer.mozilla.org/en-US/docs/Web/API/Element/attributes
440+
---
441+
442+
### 16. How can you find all leaf nodes in a given DOM tree?
443+
- Leaf nodes in a DOM tree are elements that do not have any child elements. You can traverse the tree recursively to collect all leaf nodes.
444+
445+
Implementation:
446+
```js copy
447+
function getLeafNodes(element) {
448+
let leafNodes = [];
449+
450+
// Base case: if the element has no children, it's a leaf node
451+
if (!element.children || element.children.length === 0) {
452+
leafNodes.push(element);
453+
return leafNodes;
454+
}
455+
456+
// Recursive case: traverse through each child
457+
for (let i = 0; i < element.children.length; i++) {
458+
leafNodes = leafNodes.concat(getLeafNodes(element.children[i]));
459+
}
460+
461+
return leafNodes;
462+
}
463+
464+
// Example usage:
465+
const leafNodes = getLeafNodes(document.body);
466+
console.log('Leaf nodes:', leafNodes);
467+
468+
```
469+
470+
In this implementation:
471+
472+
The function checks if the current element has no children. If so, it adds it to the leafNodes array.
473+
Otherwise, it recursively collects leaf nodes from each child.
474+
475+
**References**
476+
477+
- https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/children
478+
---
479+
480+

web/src/pages/concepts/primitives.mdx

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,4 +442,57 @@ console.log(unicodeSmileyExample);
442442
const filePathExample = "Path: C:\\\\Program Files\\\\MyApp";
443443
console.log(filePathExample);
444444
// Output: Path: C:\\Program Files\\MyApp
445-
```
445+
```
446+
---
447+
### 15. Show the usage of instanceof operator on different types of objects
448+
The instanceof operator checks if an object is an instance of a specific class or constructor function.
449+
450+
```javascript
451+
console.log([] instanceof Array); // true
452+
console.log({} instanceof Object); // true
453+
console.log(function() {} instanceof Function); // true
454+
console.log(new Date() instanceof Date); // true
455+
console.log("text" instanceof String); // false (primitive string, not an object)
456+
console.log(new String("text") instanceof String); // true (String object)
457+
```
458+
**References**
459+
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof
460+
---
461+
### 16. Demonstrate the usage of Array.isArray() to check for arrays
462+
The Array.isArray() method determines whether the provided value is an array.
463+
464+
```javascript
465+
466+
console.log(Array.isArray([])); // true
467+
console.log(Array.isArray({})); // false
468+
console.log(Array.isArray("string")); // false
469+
console.log(Array.isArray(new Array())); // true
470+
```
471+
**References**
472+
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
473+
---
474+
475+
### 17. Show how to create and use a WeakMap with objects
476+
WeakMaps are collections of key-value pairs where keys are objects and can be garbage-collected if there are no other references.
477+
478+
```javascript
479+
480+
const weakMap = new WeakMap();
481+
482+
let obj1 = {};
483+
let obj2 = {};
484+
485+
weakMap.set(obj1, "Object 1");
486+
weakMap.set(obj2, "Object 2");
487+
488+
console.log(weakMap.get(obj1)); // "Object 1"
489+
console.log(weakMap.get(obj2)); // "Object 2"
490+
491+
obj1 = null; // obj1 can be garbage collected now
492+
// weakMap entry for obj1 will also be removed automatically
493+
```
494+
**References**
495+
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap
496+
---
497+
498+

web/src/pages/interview_questions/faq_js.mdx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,3 +456,56 @@
456456
- Dynamic typing allows variables to hold values of any type without explicit type declaration, enabling type checking at runtime.
457457

458458

459+
### 31. What are the main differences between ES5 and ES6?
460+
1. Variable Declarations
461+
ES5: Uses var (function-scoped).
462+
ES6: Introduces let and const (block-scoped).
463+
464+
2. Arrow Functions
465+
ES5: Regular functions with function.
466+
ES6: Arrow functions (() => {}) with lexical this.
467+
468+
3. Template Literals
469+
ES5: String concatenation with +.
470+
ES6: Template literals using backticks for interpolation.
471+
472+
4. Destructuring Assignment
473+
ES5: Manual extraction of values.
474+
ES6: Simplified destructuring for arrays and objects.
475+
476+
5. Modules
477+
ES5: No native support; relies on libraries.
478+
ES6: Native import and export for modules.
479+
480+
6. Promises
481+
ES5: Callbacks for async operations.
482+
ES6: Promises for better async handling.
483+
484+
7. Iterators and Generators
485+
ES5: No support for iterators.
486+
ES6: Introduces iterators and generator functions.
487+
488+
### 32. How do you implement inheritance in JavaScript?
489+
490+
Inheritance can be implemented using prototypes or the class syntax introduced in ES6.
491+
```javascript
492+
function Animal(name) {
493+
this.name = name;
494+
}
495+
Animal.prototype.speak = function() {
496+
console.log(`${this.name} makes a noise.`);
497+
};
498+
499+
function Dog(name) {
500+
Animal.call(this, name);
501+
}
502+
Dog.prototype = Object.create(Animal.prototype);
503+
Dog.prototype.bark = function() {
504+
console.log(`${this.name} barks.`);
505+
};
506+
```
507+
---
508+
### 33. What is a memory leak in JavaScript, and how can you avoid it?
509+
- A memory leak occurs when memory that is no longer needed is not released. Common causes include global variables, closures, and forgotten timers.
510+
You can avoid memory leaks by ensuring proper cleanup of unused objects and avoiding unnecessary global variables.
511+
---

0 commit comments

Comments
 (0)