Skip to content

Commit 723628e

Browse files
committed
feat: Solved 10 Leetcode problems
1 parent 6a7225c commit 723628e

File tree

92 files changed

+1466
-831
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+1466
-831
lines changed

30-days-of-javascript/basic-array-transformations/easy/apply-transform-for-each-element.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
* @param {Function} fn
44
* @return {number[]}
55
*/
6-
var map = function (arr, fn)
7-
{
6+
var map = function (arr, fn) {
87
const newArray = new Array(arr.length);
98
arr.forEach((element, i) => newArray[i] = fn(element, i));
109
return newArray;

30-days-of-javascript/basic-array-transformations/easy/array-resuce-transformation.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
* @param {number} init
55
* @return {number}
66
*/
7-
var reduce = function (nums, fn, init)
8-
{
7+
var reduce = function (nums, fn, init) {
98
let accum = init;
10-
nums.forEach((element, _) =>
11-
{
9+
nums.forEach((element, _) => {
1210
accum = fn(accum, element);
1311
});
1412
return accum;

30-days-of-javascript/basic-array-transformations/easy/filter-elements-from-array.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
* @param {Function} fn
44
* @return {number[]}
55
*/
6-
var filter = function (arr, fn)
7-
{
6+
var filter = function (arr, fn) {
87
const newArr = [];
9-
arr.forEach((element, i) =>
10-
{
8+
arr.forEach((element, i) => {
119
if (fn(element, i))
1210
newArr.push(element);
1311
});

30-days-of-javascript/classes/easy/array-wrapper.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
* @param {number[]} nums
33
* @return {void}
44
*/
5-
var ArrayWrapper = function(nums) {
6-
this.nums=nums
7-
this.total=nums.length===0?0: nums.reduce((accum, value)=>accum+=value)
5+
var ArrayWrapper = function (nums) {
6+
this.nums = nums
7+
this.total = nums.length === 0 ? 0 : nums.reduce((accum, value) => accum += value)
88
};
99

1010
/**
1111
* @return {number}
1212
*/
13-
ArrayWrapper.prototype.valueOf = function() {
13+
ArrayWrapper.prototype.valueOf = function () {
1414
return this.total
1515
}
1616

1717
/**
1818
* @return {string}
1919
*/
20-
ArrayWrapper.prototype.toString = function() {
21-
return "[" +this.nums + "]"
20+
ArrayWrapper.prototype.toString = function () {
21+
return "[" + this.nums + "]"
2222
}
2323

2424
/**

30-days-of-javascript/classes/easy/calculator-with-method-chaining.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,33 @@ class Calculator {
55
* @param {number} value
66
*/
77
constructor(value) {
8-
this.#value=value
8+
this.#value = value
99
}
1010

1111
/**
1212
* @param {number} value
1313
* @return {Calculator}
1414
*/
15-
add(value){
16-
this.#value+=value
15+
add(value) {
16+
this.#value += value
1717
return this
1818
}
1919

2020
/**
2121
* @param {number} value
2222
* @return {Calculator}
2323
*/
24-
subtract(value){
25-
this.#value-=value
26-
return this
24+
subtract(value) {
25+
this.#value -= value
26+
return this
2727
}
2828

2929
/**
3030
* @param {number} value
3131
* @return {Calculator}
3232
*/
3333
multiply(value) {
34-
this.#value*=value
34+
this.#value *= value
3535
return this
3636
}
3737

@@ -40,10 +40,10 @@ class Calculator {
4040
* @return {Calculator}
4141
*/
4242
divide(value) {
43-
if(value===0)
43+
if (value === 0)
4444
throw new Error("Division by zero is not allowed")
4545

46-
this.#value/=value
46+
this.#value /= value
4747
return this
4848
}
4949

@@ -52,14 +52,14 @@ class Calculator {
5252
* @return {Calculator}
5353
*/
5454
power(value) {
55-
this.#value**=value
55+
this.#value **= value
5656
return this
5757
}
5858

5959
/**
6060
* @return {number}
6161
*/
6262
getResult() {
63-
return this.#value
63+
return this.#value
6464
}
6565
}

30-days-of-javascript/classes/medium/event-emitter.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ class EventEmitter {
22
#map
33
#lastId
44

5-
constructor()
6-
{
7-
this.#map=new Map()
8-
this.#lastId=new Map()
5+
constructor() {
6+
this.#map = new Map()
7+
this.#lastId = new Map()
98
}
109

1110
/**
@@ -14,20 +13,20 @@ class EventEmitter {
1413
* @return {Object}
1514
*/
1615
subscribe(eventName, callback) {
17-
const id=this.#map.has(eventName)?this.#lastId.get(eventName)+1:0
16+
const id = this.#map.has(eventName) ? this.#lastId.get(eventName) + 1 : 0
1817

19-
if(id!==0)
20-
this.#lastId.set(eventName,id)
18+
if (id !== 0)
19+
this.#lastId.set(eventName, id)
2120

22-
else{
21+
else {
2322
this.#lastId.set(eventName, id)
2423
this.#map.set(eventName, new Map())
2524
}
2625

2726
this.#map.get(eventName).set(id, callback)
2827

2928
return {
30-
unsubscribe: () =>this.#map.get(eventName).delete(id)
29+
unsubscribe: () => this.#map.get(eventName).delete(id)
3130
};
3231
}
3332

@@ -37,14 +36,14 @@ class EventEmitter {
3736
* @return {Array}
3837
*/
3938
emit(eventName, args = []) {
40-
const results= new Array()
41-
const callbacks=this.#map.get(eventName)
39+
const results = new Array()
40+
const callbacks = this.#map.get(eventName)
4241

43-
if(callbacks){
44-
const iter=callbacks.values()
42+
if (callbacks) {
43+
const iter = callbacks.values()
4544
let value
4645

47-
while(value=iter.next().value)
46+
while (value = iter.next().value)
4847
results.push(value(...args))
4948
}
5049

30-days-of-javascript/closures/easy/counter-ii.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
* @param {integer} init
33
* @return { increment: Function, decrement: Function, reset: Function }
44
*/
5-
var createCounter = function (init)
6-
{
5+
var createCounter = function (init) {
76
const def = init;
87

98
return {

30-days-of-javascript/closures/easy/counter.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22
* @param {number} n
33
* @return {Function} counter
44
*/
5-
var createCounter = function (n)
6-
{
5+
var createCounter = function (n) {
76
let counter = n;
87

9-
return function ()
10-
{
8+
return function () {
119
return counter++;
1210
};
1311
};
1412

15-
/**
13+
/**
1614
* const counter = createCounter(10)
1715
* counter() // 10
1816
* counter() // 11

30-days-of-javascript/closures/easy/create-hello-world-function.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
/**
22
* @return {Function}
33
*/
4-
function createHelloWorld ()
5-
{
6-
return function ()
7-
{
4+
function createHelloWorld() {
5+
return function () {
86
return "Hello World";
97
};
108
};

30-days-of-javascript/closures/easy/to-be-or-not-to-be.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22
* @param {string} val
33
* @return {Object}
44
*/
5-
var expect = function (val)
6-
{
5+
var expect = function (val) {
76
const expectObject = {};
87

9-
expectObject.toBe = function (valToBe)
10-
{
8+
expectObject.toBe = function (valToBe) {
119
if (valToBe !== val)
1210
throw new Error("Not Equal");
1311
return true;
1412
};
1513

16-
expectObject.notToBe = function notToBe (valNotToBe)
17-
{
14+
expectObject.notToBe = function notToBe(valNotToBe) {
1815
if (valNotToBe === val)
1916
throw new Error("Equal");
2017
return true;

30-days-of-javascript/function-transformations/easy/allow-one-function-call.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
* @param {Function} fn
33
* @return {Function}
44
*/
5-
var once = function (fn)
6-
{
5+
var once = function (fn) {
76
let called = false;
87

9-
return function (...args)
10-
{
8+
return function (...args) {
119
if (called)
1210
return undefined;
1311

30-days-of-javascript/function-transformations/easy/function-composition.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
* @param {Function[]} functions
33
* @return {Function}
44
*/
5-
var compose = function (functions)
6-
{
7-
return function (x)
8-
{
5+
var compose = function (functions) {
6+
return function (x) {
97
for (let i = functions.length - 1; i >= 0; i--)
108
x = functions[i](x);
119
return x;

30-days-of-javascript/function-transformations/easy/length-of-arguments-passed.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
* @param {...(null|boolean|number|string|Array|Object)} args
33
* @return {number}
44
*/
5-
var argumentsLength = function (...args)
6-
{
5+
var argumentsLength = function (...args) {
76
return args.length;
87
};
98

30-days-of-javascript/function-transformations/medium/memoize.js

+10-16
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,21 @@
22
* @param {Function} fn
33
* @return {Function}
44
*/
5-
function memoize (fn)
6-
{
5+
function memoize(fn) {
76
const map = new Map();
87

9-
return function (...args)
10-
{
8+
return function (...args) {
119
let result;
1210

13-
if (args.length === 2)
14-
{
11+
if (args.length === 2) {
1512
if (map.has(args[0]))
1613
result = map.get(args[0]).get(args[1]);
17-
else
18-
{
14+
else {
1915
map.set(args[0], new Map());
2016
result = undefined;
2117
}
2218

23-
if (result === undefined)
24-
{
19+
if (result === undefined) {
2520
result = fn(...args);
2621
map.get(args[0]).set(args[1], result);
2722
}
@@ -30,22 +25,21 @@ function memoize (fn)
3025

3126
result = map.get(args[0]);
3227

33-
if (result === undefined)
34-
{
28+
if (result === undefined) {
3529
result = fn(args[0]);
3630
map.set(args[0], result);
3731
}
3832
return result;
3933
};
4034
}
4135

42-
/**
36+
/**
4337
* let callCount = -1;
4438
* const memoizedFn = memoize(function (a, b) {
45-
* callCount += 1;
39+
* callCount += 1;
4640
* return a + b;
47-
* })
41+
* })
4842
* memoizedFn(2, 3) // 5
4943
* memoizedFn(2, 3) // 5
50-
* console.log(callCount) // 1
44+
* console.log(callCount) // 1
5145
*/

30-days-of-javascript/json/easy/array-prototype-last.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* @return {null|boolean|number|string|Array|Object}
33
*/
4-
Array.prototype.last = function() {
5-
return this.length===0?-1:this[this.length-1];
4+
Array.prototype.last = function () {
5+
return this.length === 0 ? -1 : this[this.length - 1];
66
};
77

88
/**

0 commit comments

Comments
 (0)