30 JavaScript Basic Exercises for Intermediate with Solutions

Master intermediate 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 advanced challenges. Start your journey to JavaScript mastery today!

Learning Objectives:

By the end of these exercises, you will be proficient in intermediate JavaScript concepts, such as working with arrays, objects, event handling, and asynchronous programming.

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 returns the nth Fibonacci number.

Required Input:

n = 5

Expected Output:

5

Code In Javascript

// Find the nth Fibonacci number function fibonacci(n) { } console.log(fibonacci(5));

Run Code?

Click Run Button to view compiled output

2. Create a function to calculate the greatest common divisor (GCD) of two numbers.

Required Input:

a = 48, b = 18

Expected Output:

6

Code In Javascript

// Calculate GCD using Euclidean algorithm function gcd(a, b) { } console.log(gcd(48, 18));

Run Code?

Click Run Button to view compiled output

3. Write a program to count the occurrences of each character in a string.

Required Input:

"hello"

Expected Output:

{ h: 1, e: 1, l: 2, o: 1 }

Code In Javascript

// Count occurrences of each character function countCharacters(str) { } console.log(countCharacters("hello"));

Run Code?

Click Run Button to view compiled output

4. Create a function that accepts a string and returns a new string with vowels removed.

Required Input:

"javascript"

Expected Output:

jvscrpt

Code In Javascript

// Remove vowels from a string function removeVowels(str) { } console.log(removeVowels("javascript"));

Run Code?

Click Run Button to view compiled output

5. Write a function to find the second smallest number in an array.

Required Input:

[4, 2, 9, 7, 5]

Expected Output:

4

Code In Javascript

// Find the second smallest number in an array function secondSmallest(arr) { return sortedArr[]; } console.log(secondSmallest([4, 2, 9, 7, 5]));

Run Code?

Click Run Button to view compiled output

6. Implement a function that checks if a given string is an anagram of another string.

Required Input:

str1 = "listen", str2 = "silent"

Expected Output:

true

Code In Javascript

// Check if two strings are anagrams function areAnagrams(str1, str2) { } console.log(areAnagrams("listen", "silent"));

Run Code?

Click Run Button to view compiled output

7. Create a function to find the unique elements in an array of numbers.

Required Input:

[1, 2, 3, 2, 4, 1, 5]

Expected Output:

[ 1, 2, 3, 4, 5 ]

Code In Javascript

// Find unique elements in an array function uniqueElements(arr) { } console.log(uniqueElements([1, 2, 3, 2, 4, 1, 5]));

Run Code?

Click Run Button to view compiled output

8. Write a program to flatten a nested array (e.g., [1, [2, [3, 4]], 5] should become [1, 2, 3, 4, 5]).

Required Input:

[1, [2, [3, 4]], 5]

Expected Output:

[ 1, 2, 3, 4, 5 ]

Code In Javascript

// Flatten a nested array function flattenArray(arr) { } console.log(flattenArray([1, [2, [3, 4]], 5]));

Run Code?

Click Run Button to view compiled output

9. Create a function to convert a string to "snake_case" format.

Required Input:

"JavaScript Is Fun"

Expected Output:

javascript_is_fun

Code In Javascript

// Convert string to snake_case function toSnakeCase(str) { } console.log(toSnakeCase("JavaScript Is Fun"));

Run Code?

Click Run Button to view compiled output

10. Write a program to find the longest word in a sentence.

Required Input:

"Coding is a wonderful experience"

Expected Output:

experience

Code In Javascript

// Find the longest word in a sentence function longestWord(sentence) { } console.log(longestWord("Coding is a wonderful experience"));

Run Code?

Click Run Button to view compiled output

ad vertical

1 of 3