Skip to content

Commit 5222c10

Browse files
committed
added today practice
1 parent b279e64 commit 5222c10

File tree

5 files changed

+144
-49
lines changed

5 files changed

+144
-49
lines changed

AddTwoPromise.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Given two promises promise1 and promise2, return a new promise. promise1 and promise2 will both resolve with a number. The returned promise should resolve with the sum of the two numbers.
2+
3+
// promise1 = new Promise(resolve => setTimeout(() => resolve(2), 20)),
4+
// promise2 = new Promise(resolve => setTimeout(() => resolve(5), 60))
5+
// Output: 7
6+
// Explanation: The two input promises resolve with the values of 2 and 5 respectively. The returned promise should resolve with a value of 2 + 5 = 7. The time the returned promise resolves is not judged for this problem.
7+
8+
/**
9+
* @param {Promise} promise1
10+
* @param {Promise} promise2
11+
* @return {Promise}
12+
*/
13+
var addTwoPromises = async function (promise1, promise2) {
14+
let sum = await Promise.all([promise1, promise2]).then(([num1, num2]) => num1 + num2);
15+
return sum;
16+
17+
18+
};
19+
20+
// Both are same only logic has been different
21+
22+
var addTwoPromises = async function (promise1, promise2) {
23+
const val1 = await promise1
24+
const val2 = await promise2
25+
26+
return val1 + val2
27+
};

AllowOneFunctionCall.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Given a function fn, return a new function that is identical to the original function except that it ensures fn is called at most once.
2+
3+
// The first time the returned function is called, it should return the same result as fn.
4+
// Every subsequent time it is called, it should return undefined.
5+
6+
/**
7+
* @param {Function} fn
8+
* @return {Function}
9+
*/
10+
var once = function(fn) {
11+
let flag=false;
12+
return function(...args){
13+
if(flag===false){
14+
flag=true
15+
return fn(...args)
16+
}else{
17+
return undefined
18+
}
19+
}
20+
};

Memoize.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Given a function fn, return a memoized version of that function.
2+
3+
// A memoized function is a function that will never be called twice with the same inputs. Instead it will return a cached value.
4+
5+
// You can assume there are 3 possible input functions: sum, fib, and factorial.
6+
7+
// sum accepts two integers a and b and returns a + b. Assume that if a value has already been cached for the arguments (b, a) where a != b, it cannot be used for the arguments (a, b). For example, if the arguments are (3, 2) and (2, 3), two separate calls should be made.
8+
// fib accepts a single integer n and returns 1 if n <= 1 or fib(n - 1) + fib(n - 2) otherwise.
9+
// factorial accepts a single integer n and returns 1 if n <= 1 or factorial(n - 1) * n otherwise
10+
11+
12+
function memoized(fn) {
13+
const cachedVal = {}
14+
return function (...args) {
15+
const val = args
16+
if (val in cachedVal) {
17+
return cachedVal[val]
18+
}
19+
else {
20+
const result = fn(...args)
21+
cachedVal[val] = result
22+
return result
23+
}
24+
}
25+
}

README.md

Lines changed: 70 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,71 @@
1-
<p align="center"><h1 font="10rem" align="center">30 Days JavaScript Challenge</h1>
2-
Welcome to the 30 Days JavaScript Challenge! This repository is designed to help you improve your JavaScript skills through daily coding exercises. Each day, you'll tackle a new challenge to enhance your understanding of core JavaScript concepts.
3-
4-
![First loading Page](https://github.com/ashish8513/30-days-javascript-leetCode-challenges/blob/main/1day.png)
5-
6-
Challenge Overview
7-
Day 1: Basic Syntax
8-
Task: Write a function to print "Hello, World!" to the console.
9-
Objective: Understand basic syntax and function creation.
10-
Day 2: Variables and Data Types
11-
Task: Create a program that declares different types of variables and prints them.
12-
Objective: Learn about data types and variable declaration.
13-
Day 3: Conditional Statements
14-
Task: Implement a function that checks if a number is positive, negative, or zero.
15-
Objective: Practice using if-else statements.
16-
Day 4: Loops
17-
Task: Write a function that prints numbers 1 to 10 using a loop.
18-
Objective: Understand for and while loops.
19-
Day 5: Functions
20-
Task: Create a function that calculates the factorial of a number.
21-
Objective: Learn function definition and recursion.
22-
Day 6: Arrays
23-
Task: Write a program that finds the largest number in an array.
24-
Objective: Practice working with arrays and array methods.
25-
Day 7: Objects
26-
Task: Create an object representing a person and print its properties.
27-
Objective: Understand object creation and property access.
28-
Day 8: String Manipulation
29-
Task: Implement a function that reverses a string.
30-
Objective: Learn string methods and manipulation.
31-
Day 9: DOM Manipulation
32-
Task: Write a script that changes the background color of a webpage when a button is clicked.
33-
Objective: Practice manipulating the DOM with JavaScript.
34-
Day 10: Event Handling
35-
Task: Create a form that validates user input and displays a message when submitted.
36-
Objective: Understand event handling and form validation.
37-
Day 11-30: [Continue with more advanced topics like Promises, Async/Await, APIs, Error Handling, etc.]
38-
39-
40-
![First loading Page](https://github.com/ashish8513/30-days-javascript-leetCode-challenges/blob/main/leetcode.png)
41-
42-
Consistency: Try to complete each day's challenge on the assigned day.
43-
Practice: Don't hesitate to revisit challenges or seek additional resources if needed.
44-
Collaboration: Engage with the community for support and feedback.
45-
46-
![First loading Page](https://github.com/ashish8513/30-days-javascript-leetCode-challenges/blob/main/vs.png)
47-
48-
49-
For Any Query Please contact me [email protected]
1+
<p align="center">
2+
<img src="https://github.com/ashish8513/30-days-javascript-leetCode-challenges/blob/main/1day.png" alt="Challenge Overview" width="600"/>
503
</p>
4+
5+
<h1 align="center" style="font-size: 3rem;">30 Days JavaScript Challenge</h1>
6+
7+
<p align="center">
8+
Welcome to the 30 Days JavaScript Challenge! This repository is designed to help you improve your JavaScript skills through daily coding exercises. Each day, you'll tackle a new challenge to enhance your understanding of core JavaScript concepts.
9+
</p>
10+
11+
## Challenge Overview
12+
13+
**Day 1: Basic Syntax**
14+
- **Task**: Write a function to print "Hello, World!" to the console.
15+
- **Objective**: Understand basic syntax and function creation.
16+
17+
**Day 2: Variables and Data Types**
18+
- **Task**: Create a program that declares different types of variables and prints them.
19+
- **Objective**: Learn about data types and variable declaration.
20+
21+
**Day 3: Conditional Statements**
22+
- **Task**: Implement a function that checks if a number is positive, negative, or zero.
23+
- **Objective**: Practice using if-else statements.
24+
25+
**Day 4: Loops**
26+
- **Task**: Write a function that prints numbers 1 to 10 using a loop.
27+
- **Objective**: Understand for and while loops.
28+
29+
**Day 5: Functions**
30+
- **Task**: Create a function that calculates the factorial of a number.
31+
- **Objective**: Learn function definition and recursion.
32+
33+
**Day 6: Arrays**
34+
- **Task**: Write a program that finds the largest number in an array.
35+
- **Objective**: Practice working with arrays and array methods.
36+
37+
**Day 7: Objects**
38+
- **Task**: Create an object representing a person and print its properties.
39+
- **Objective**: Understand object creation and property access.
40+
41+
**Day 8: String Manipulation**
42+
- **Task**: Implement a function that reverses a string.
43+
- **Objective**: Learn string methods and manipulation.
44+
45+
**Day 9: DOM Manipulation**
46+
- **Task**: Write a script that changes the background color of a webpage when a button is clicked.
47+
- **Objective**: Practice manipulating the DOM with JavaScript.
48+
49+
**Day 10: Event Handling**
50+
- **Task**: Create a form that validates user input and displays a message when submitted.
51+
- **Objective**: Understand event handling and form validation.
52+
53+
**Day 11-30**: [Continue with more advanced topics like Promises, Async/Await, APIs, Error Handling, etc.]
54+
55+
<p align="center">
56+
<img src="https://github.com/ashish8513/30-days-javascript-leetCode-challenges/blob/main/leetcode.png" alt="LeetCode" width="600"/>
57+
</p>
58+
59+
## Tips for Success
60+
61+
- **Consistency**: Try to complete each day's challenge on the assigned day.
62+
- **Practice**: Don't hesitate to revisit challenges or seek additional resources if needed.
63+
- **Collaboration**: Engage with the community for support and feedback.
64+
65+
<p align="center">
66+
<img src="https://github.com/ashish8513/30-days-javascript-leetCode-challenges/blob/main/vs.png" alt="Visual Studio" width="600"/>
67+
</p>
68+
69+
## Contact
70+
71+
For any queries, please contact me at [[email protected]](mailto:[email protected])

Sleep.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Given a positive integer millis, write an asynchronous function that sleeps for millis milliseconds. It can resolve any value.
2+

0 commit comments

Comments
 (0)