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

// Memoize a function to improve performance function memoize(fn) { // Create a cache object // Return a function that checks if the result is in the cache // If not, calculate, store, and return the result } // Example function to memoize function fibonacci(n) { // Base cases for fibonacci // Recursive call for fibonacci sequence } const memoizedFibonacci = memoize(fibonacci); console.log(memoizedFibonacci(10));

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

// Calculate nth Fibonacci number using dynamic programming function fibonacciDP(n) { // Initialize an array with base cases // Iterate up to n, storing results in the array // Return the nth result } console.log(fibonacciDP(10));

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

// Deep clone an object, including nested structures function deepClone(obj) { // Check if the value is not an object or is null, return it // If it is an array, map each element using deepClone // For an object, iterate and deepClone each property } const original = { a: 1, b: [ 1, 2, 3 ], c: { d: 4, e: 5 } }; const cloned = deepClone(original); console.log(cloned);

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

// Perform binary search on a sorted array function binarySearch(arr, target) { // Initialize left and right pointers // Use a loop to find the target by adjusting pointers // Return the index if found, otherwise -1 } console.log(binarySearch([ 1, 3, 5, 7, 9, 11 ], 7));

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

// Implement debouncing to delay function execution function debounce(func, delay) { // Initialize a timer variable // Return a function that clears the timer and sets a new one } // Example function to debounce function logMessage() { // Log a message to the console } const debouncedLog = debounce(logMessage, 300); debouncedLog(); debouncedLog(); debouncedLog();

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

// Generate all permutations of a given string function permute(str) { // Base case: return the string if length is 1 or less // Loop through characters and generate permutations recursively } console.log(permute("abc"));

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

// Check if two objects are deeply equal function deepEqual(obj1, obj2) { // Check for reference equality // If either is not an object, compare values directly // Compare keys and recursively compare properties } console.log(deepEqual({ a: 1, b: { c: 2 } }, { a: 1, b: { c: 2 } }));

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

// Generate all subsets of an array function getSubsets(arr) { // Initialize an array with an empty subset // Loop through elements, generating new subsets by adding each element to existing subsets } console.log(getSubsets([ 1, 2, 3 ]));

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

// Generate all combinations of a string with a specific length function getCombinations(str, length, start = 0, current = '', combinations = [ ]) { // Check if current combination has the required length // Loop through characters, recursively forming combinations } console.log(getCombinations("abc", 2));

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

// Implement rate limiting on a function function rateLimiter(func, limit) { // Initialize variable to track last executed time // Return a function that checks time elapsed and executes func accordingly } // Example function to limit function logMessage() { // Log a message to the console } const limitedLog = rateLimiter(logMessage, 1000); limitedLog(); limitedLog(); limitedLog();

Run Code?

Click Run Button to view compiled output

ad vertical

1 of 3