30 JavaScript Basic Exercises for Advanced with Solutions
Master advanced JavaScript skills with our comprehensive list of top 30 exercises. Dive into coding challenges that improve your understanding and proficiency in JavaScript, setting a solid foundation for professional-level challenges. Start your journey to JavaScript mastery today!
Learning Objectives:
By the end of these exercises, you will have mastered advanced JavaScript topics, including closures, ES6+ features, promises, and optimizing DOM manipulation.
Exercise Instructions:
- Start with the first exercise and attempt to solve it before checking the hint or solution.
- Ensure you understand the logic behind each solution, as this will help you in more complex problems.
- Use these exercises to reinforce your learning and identify areas that may require further study.
1. Write a function that memoizes another function to improve performance.
Required Input:
Function to be memoized: fibonacci
Argument for the memoized function: 10
Expected Output:
55
Code In Javascript
Run Code?
Click Run Button to view compiled output
2. Implement a function to calculate the nth number in the Fibonacci sequence using dynamic programming.
Required Input:
n = 10
Expected Output:
55
Code In Javascript
Run Code?
Click Run Button to view compiled output
3. Create a function that deep clones an object, including nested objects and arrays.
Required Input:
{
a: 1,
b: [ 1, 2, 3 ],
c: { d: 4, e: 5 } }
Expected Output:
{ a: 1, b: [ 1, 2, 3 ], c: { d: 4, e: 5 } }
Code In Javascript
Run Code?
Click Run Button to view compiled output
4. Write a function to perform a binary search on a sorted array.
Required Input:
array = [ 1, 3, 5, 7, 9, 11 ], target = 7
Expected Output:
3
Code In Javascript
Run Code?
Click Run Button to view compiled output
5. Implement a function to debounce another function, delaying its execution until after a specified interval.
Required Input:
Function to be debounced: logMessage
Debounce interval: 300 ms
Number of times logMessage is called in a row: 3
Expected Output:
Only one output after 300ms of inactivity.
Code In Javascript
Run Code?
Click Run Button to view compiled output
6. Create a function that returns the permutations of a given string.
Required Input:
"abc"
Expected Output:
["abc","acb","bac","bca","cab","cba"]
Code In Javascript
Run Code?
Click Run Button to view compiled output
7. Write a function that determines if two objects are deeply equal.
Required Input:
obj1 = { a: 1, b: { c: 2 } }
obj2 = { a: 1, b: { c: 2 } }
Expected Output:
true
Code In Javascript
Run Code?
Click Run Button to view compiled output
8. Implement a function that returns all subsets of a given array.
Required Input:
[ 1, 2, 3 ]
Expected Output:
[ [], [ 1 ], [ 2 ], [ 3 ], [ 1, 2 ], [ 1, 3 ], [ 2, 3 ], [ 1, 2, 3 ] ]
Code In Javascript
Run Code?
Click Run Button to view compiled output
9. Write a function to generate all combinations of a string with a given length.
Required Input:
str = "abc", length = 2
Expected Output:
[ 'ab', 'ac', 'bc' ]
Code In Javascript
Run Code?
Click Run Button to view compiled output
10. Create a function that implements a rate limiter for a given function, limiting its execution to once every specified interval.
Required Input:
Function to be rate limited: logMessage
Rate limit interval: 1000 ms
Number of calls to logMessage: 5
Expected Output:
Only one output every 1000ms.
Code In Javascript
Run Code?
Click Run Button to view compiled output