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
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
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
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
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
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
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
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
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
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
Run Code?
Click Run Button to view compiled output