Skip to content

Commit 5396da9

Browse files
BarklimBarklim
authored andcommitted
add js challenge
1 parent a26f889 commit 5396da9

File tree

9 files changed

+280
-0
lines changed

9 files changed

+280
-0
lines changed

example/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,17 @@ Better order to solve problems
270270
1041. Robot Bounded In Circle
271271
1572. Matrix Diagonal Sum
272272

273+
## 30 Days of JavaScript
274+
275+
2667. Create Hello World Function
276+
2620. Counter
277+
2704. To Be Or Not To Be
278+
2665. Counter II
279+
2635. Apply Transform Over Each Element in Array
280+
2634. Filter Elements from Array
281+
2626. Array Reduce Transformation
282+
2629. Function Composition
283+
273284
### [Strong List](structy.net)
274285

275286
1. Depth-First Search (DFS)

example/js/2620.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* 2620. Counter
3+
* https://leetcode.com/problems/counter/
4+
* Difficulty: Easy
5+
*
6+
* Given an integer n, return a counter function. This counter function initially
7+
* returns n and then returns 1 more than the previous value every subsequent
8+
* time it is called (n, n + 1, n + 2, etc).
9+
*/
10+
11+
/**
12+
* @param {number} n
13+
* @return {Function} counter
14+
*/
15+
var createCounter = function(n) {
16+
17+
return function() {
18+
19+
};
20+
};
21+
22+
const counter = createCounter(10)
23+
counter()
24+
counter()
25+
counter()
26+
console.log(counter()) // 13
27+
console.log(counter()) // 14

example/js/2626.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 2626. Array Reduce Transformation
3+
* https://leetcode.com/problems/array-reduce-transformation/
4+
* Difficulty: Easy
5+
*
6+
* Given an integer array nums, a reducer function fn, and an initial value init, return the final
7+
* result obtained by executing the fn function on each element of the array, sequentially,
8+
* passing in the return value from the calculation on the preceding element.
9+
*
10+
* This result is achieved through the following operations: val = fn(init, nums[0]),
11+
* val = fn(val, nums[1]), val = fn(val, nums[2]), ... until every element in the array has been
12+
* processed. The ultimate value of val is then returned.
13+
*
14+
* If the length of the array is 0, the function should return init.
15+
*
16+
* Please solve it without using the built-in Array.reduce method.
17+
*/
18+
19+
/**
20+
* @param {number[]} nums
21+
* @param {Function} fn
22+
* @param {number} init
23+
* @return {number}
24+
*/
25+
var reduce = function(nums, fn, init) {
26+
27+
};
28+
29+
30+
function sum1(accum, curr) { return accum + curr; }
31+
ex1 = reduce([1,2,3,4], sum1, 0)
32+
33+
function sum2(accum, curr) { return accum + curr * curr; }
34+
ex2 = reduce([1,2,3,4], sum2, 100)
35+
36+
function sum3(accum, curr) { return 0; }
37+
ex3 = reduce([], sum3, 25)
38+
39+
console.log(ex1) // 10
40+
console.log(ex2) // 130
41+
console.log(ex3) // 25
42+

example/js/2629.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* 2629. Function Composition
3+
* https://leetcode.com/problems/function-composition/
4+
* Difficulty: Easy
5+
*
6+
* Given an array of functions [f1, f2, f3, ..., fn], return a new function fn that is the function
7+
* composition of the array of functions.
8+
*
9+
* The function composition of [f(x), g(x), h(x)] is fn(x) = f(g(h(x))).
10+
*
11+
* The function composition of an empty list of functions is the identity function f(x) = x.
12+
*
13+
* You may assume each function in the array accepts one integer as input and returns one integer
14+
* as output.
15+
*/
16+
17+
/**
18+
* @param {Function[]} functions
19+
* @return {Function}
20+
*/
21+
var compose = function(functions) {
22+
23+
return function(x) {
24+
25+
}
26+
};
27+
28+
29+
functions1 = [x => x + 1, x => x * x, x => 2 * x]
30+
ex1 = compose(functions1)(4)
31+
32+
functions2 = [x => 10 * x, x => 10 * x, x => 10 * x]
33+
ex2 = compose(functions2)(1)
34+
35+
functions3 = []
36+
ex3 = compose(functions3)(42)
37+
38+
console.log(ex1) // 65
39+
console.log(ex2) // 1000
40+
console.log(ex3) // 42

example/js/2634.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 2634. Filter Elements from Array
3+
* https://leetcode.com/problems/filter-elements-from-array/
4+
* Difficulty: Easy
5+
*
6+
* Given an integer array arr and a filtering function fn, return a filtered array filteredArr.
7+
*
8+
* The fn function takes one or two arguments:
9+
* arr[i] - number from the arr
10+
* i - index of arr[i]
11+
*
12+
* filteredArr should only contain the elements from the arr for which the expression
13+
* fn(arr[i], i) evaluates to a truthy value. A truthy value is a value where
14+
* Boolean(value) returns true.
15+
*
16+
* Please solve it without the built-in Array.filter method.
17+
*/
18+
19+
/**
20+
* @param {number[]} arr
21+
* @param {Function} fn
22+
* @return {number[]}
23+
*/
24+
var filter = function(arr, fn) {
25+
26+
};
27+
28+
29+
fn1 = function greaterThan10(n) { return n > 10; }
30+
ex1 = filter([0,10,20,30], fn1)
31+
32+
fn2 = function firstIndex(n, i) { return i === 0; }
33+
ex2 = filter([1,2,3], fn2)
34+
35+
fn3 = function plusOne(n) { return n + 1 }
36+
ex3 = filter([-2,-1,0,1,2], fn3)
37+
38+
console.log(ex1) // [20, 30]
39+
console.log(ex2) // [1]
40+
console.log(ex3) // [-2,0,1,2]
41+

example/js/2635.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 2635. Apply Transform Over Each Element in Array
3+
* https://leetcode.com/problems/apply-transform-over-each-element-in-array/
4+
* Difficulty: Easy
5+
*
6+
* Given an integer array arr and a mapping function fn, return a new array with a transformation
7+
* applied to each element.
8+
*
9+
* The returned array should be created such that returnedArray[i] = fn(arr[i], i).
10+
*
11+
* Please solve it without the built-in Array.map method.
12+
*/
13+
14+
/**
15+
* @param {number[]} arr
16+
* @param {Function} fn
17+
* @return {number[]}
18+
*/
19+
var map = function(arr, fn) {
20+
21+
};
22+
23+
const arr = [1,2,3]
24+
25+
fn1 = function plusone(n) { return n + 1; }
26+
ex1 = map(arr, fn1)
27+
28+
fn2 = function plusI(n, i) { return n + i; }
29+
ex2 = map(arr, fn2)
30+
31+
fn3 = function constant() { return 42; }
32+
ex3 = map(arr, fn3)
33+
34+
console.log(ex1)
35+
console.log(ex2)
36+
console.log(ex3)
37+

example/js/2655.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 2665. Counter II
3+
* https://leetcode.com/problems/counter-ii/
4+
* Difficulty: Easy
5+
*
6+
* Write a function createCounter. It should accept an initial integer init.
7+
* It should return an object with three functions.
8+
*
9+
* The three functions are:
10+
* - increment() increases the current value by 1 and then returns it.
11+
* - decrement() reduces the current value by 1 and then returns it.
12+
* - reset() sets the current value to init and then returns it.
13+
*/
14+
15+
/**
16+
* @param {integer} init
17+
* @return { increment: Function, decrement: Function, reset: Function }
18+
*/
19+
var createCounter = function(init) {
20+
21+
};
22+
23+
const counter = createCounter(5)
24+
// counter.increment(); // 6
25+
// counter.reset(); // 5
26+
// counter.decrement(); // 4
27+
28+
console.log(counter.increment()) // 6
29+
console.log(counter.increment()) // 7
30+
console.log(counter.reset()) // 5
31+
console.log(counter.decrement()) // 4
32+

example/js/2667.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* 2667. Create Hello World Function
3+
* https://leetcode.com/problems/create-hello-world-function/
4+
* Difficulty: Easy
5+
*
6+
* Write a function createHelloWorld. It should return a new function that
7+
* always returns "Hello World".
8+
*/
9+
10+
/**
11+
* @return {Function}
12+
*/
13+
var createHelloWorld = function() {
14+
15+
return function(...args) {
16+
17+
}
18+
};
19+
20+
const f = createHelloWorld();
21+
f();
22+
23+
console.log(f()) // "Hello World"

example/js/2704.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* 2704. To Be Or Not To Be
3+
* https://leetcode.com/problems/to-be-or-not-to-be/
4+
* Difficulty: Easy
5+
*
6+
* Write a function expect that helps developers test their code. It should take in any
7+
* value val and return an object with the following two functions.
8+
*
9+
* - toBe(val) accepts another value and returns true if the two values === each other.
10+
* If they are not equal, it should throw an error "Not Equal".
11+
* - notToBe(val) accepts another value and returns true if the two values !== each
12+
* other. If they are equal, it should throw an error "Equal".
13+
*/
14+
15+
/**
16+
* @param {string} val
17+
* @return {Object}
18+
*/
19+
var expect = function(val) {
20+
21+
};
22+
23+
// expect(5).toBe(5); // true
24+
// expect(5).notToBe(5); // throws "Equal"
25+
26+
console.log(expect(5).toBe(5))
27+
console.log(expect(5).notToBe(5))

0 commit comments

Comments
 (0)