Skip to content

Commit 35be27d

Browse files
Update js-pw.md
1 parent 95e7d15 commit 35be27d

1 file changed

Lines changed: 0 additions & 97 deletions

File tree

js-pw.md

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -2739,103 +2739,6 @@ console.log(romanize(3)); // III
27392739
<b><a href="#javascript-coding-practice">↥ back to top</a></b>
27402740
</div>
27412741

2742-
## Q. Write a program to check if parenthesis is malformed or not?
2743-
2744-
**Example:**
2745-
2746-
```js
2747-
Input: "}{{}}"
2748-
Output: false
2749-
2750-
Input: "{{[]}}"
2751-
Output: true
2752-
```
2753-
2754-
<details><summary><b>Answer</b></summary>
2755-
2756-
```js
2757-
function matchParenthesis(str) {
2758-
let obj = { "{": "}", "(": ")", "[": "]" };
2759-
let result = [];
2760-
for (let s of str) {
2761-
if (s === "{" || s === "(" || s === "[") {
2762-
// All opening brackets
2763-
result.push(s);
2764-
} else {
2765-
if (result.length > 0) {
2766-
let lastValue = result.pop(); // pop the last value and compare with key
2767-
if (obj[lastValue] !== s) {
2768-
// if it is not same then it is not formated properly
2769-
return false;
2770-
}
2771-
} else {
2772-
return false; // empty array, there is nothing to pop. so it is not formated properly
2773-
}
2774-
}
2775-
}
2776-
return result.length === 0;
2777-
}
2778-
2779-
console.log(matchParenthesis("}{{}}")); // false
2780-
console.log(matchParenthesis("{{[]}}")); // true
2781-
```
2782-
2783-
</details>
2784-
2785-
<div align="right">
2786-
<b><a href="#javascript-coding-practice">↥ back to top</a></b>
2787-
</div>
2788-
2789-
## Q. Create Custom Event Emitter class?
2790-
2791-
**Example:**
2792-
2793-
```js
2794-
Output:
2795-
'Custom event called with argument: a,b,[object Object]'
2796-
'Custom event called without argument'
2797-
```
2798-
2799-
<details><summary><b>Answer</b></summary>
2800-
2801-
```js
2802-
class EventEmitter {
2803-
constructor() {
2804-
this.holder = {};
2805-
}
2806-
2807-
on(eventName, fn) {
2808-
if (eventName && typeof fn === "function") {
2809-
this.holder[eventName] = this.holder[eventName] || [];
2810-
this.holder[eventName].push(fn);
2811-
}
2812-
}
2813-
2814-
emit(eventName, ...args) {
2815-
let eventColl = this.holder[eventName];
2816-
if (eventColl) {
2817-
eventColl.forEach((callback) => callback(args));
2818-
}
2819-
}
2820-
}
2821-
2822-
let e = new EventEmitter();
2823-
e.on("myCustomEvent", function (args) {
2824-
console.log(`Custom event called with arguments: ${args}`);
2825-
});
2826-
e.on("myCustomEvent", function () {
2827-
console.log(`Custom event called without argument`);
2828-
});
2829-
2830-
e.emit("myCustomEvent", ["a", "b"], { firstName: "Umesh", lastName: "Gohil" });
2831-
```
2832-
2833-
</details>
2834-
2835-
<div align="right">
2836-
<b><a href="#javascript-coding-practice">↥ back to top</a></b>
2837-
</div>
2838-
28392742
## Q. Write a program to move all zeroes to end?
28402743

28412744
**Example:**

0 commit comments

Comments
 (0)