JavaScript Coding Challenges for Practice
Introduction
Practicing coding challenges is one of the most effective ways to learn JavaScript. Challenges help beginners understand core concepts, improve problem-solving skills, and prepare for interviews or real-world projects. In this guide, we’ll explore beginner-friendly JavaScript coding challenges and tips on how to approach them effectively.
1. Reverse a String
Problem: Write a function that reverses a string.
function reverseString(str) {
return str.split("").reverse().join("");
}console.log(reverseString("CodebyHer")); // "reHyb edoC"
What You Learn:
- String manipulation
- Array methods (
split,reverse,join)
2. Check for Palindrome
Problem: Determine if a string is the same forward and backward.
function isPalindrome(str) {
const reversed = str.split("").reverse().join("");
return str === reversed;
}console.log(isPalindrome("level")); // true
console.log(isPalindrome("hello")); // false
What You Learn:
- Conditionals
- Working with strings
- Boolean logic
3. Find the Largest Number in an Array
Problem: Write a function to find the largest number in an array.
function findLargest(arr) {
return Math.max(...arr);
}console.log(findLargest([3, 7, 2, 9, 5])); // 9
What You Learn:
- Arrays and array operations
- Spread operator
- Math functions
4. Sum All Numbers in an Array
Problem: Calculate the sum of all numbers in an array.
function sumArray(arr) {
return arr.reduce((sum, num) => sum + num, 0);
}console.log(sumArray([1, 2, 3, 4, 5])); // 15
What You Learn:
- Array iteration with
reduce() - Functional programming in JavaScript
5. FizzBuzz Challenge
Problem: Print numbers from 1 to 50, but replace multiples of 3 with “Fizz”, multiples of 5 with “Buzz”, and multiples of both with “FizzBuzz”.
for (let i = 1; i <= 50; i++) {
if (i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");
} else if (i % 3 === 0) {
console.log("Fizz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else {
console.log(i);
}
}
What You Learn:
- Loops
- Conditional logic
- Modulus operator
6. Convert Celsius to Fahrenheit
Problem: Create a function to convert temperature from Celsius to Fahrenheit.
function celsiusToFahrenheit(celsius) {
return (celsius * 9/5) + 32;
}console.log(celsiusToFahrenheit(0)); // 32
console.log(celsiusToFahrenheit(100)); // 212
What You Learn:
- Functions
- Arithmetic operations
- Temperature conversions
7. Remove Duplicates from an Array
Problem: Remove duplicate elements from an array.
function removeDuplicates(arr) {
return [...new Set(arr)];
}console.log(removeDuplicates([1, 2, 2, 3, 3, 4])); // [1, 2, 3, 4]
What You Learn:
- Sets in JavaScript
- Spread operator
- Array manipulation
8. Count Vowels in a String
Problem: Count how many vowels are in a string.
function countVowels(str) {
return str.match(/[aeiou]/gi)?.length || 0;
}console.log(countVowels("CodebyHer")); // 4
What You Learn:
- Regular expressions
- String manipulation
- Null-safe operators
9. Factorial of a Number
Problem: Calculate the factorial of a number using a function.
function factorial(n) {
if (n === 0 || n === 1) return 1;
return n * factorial(n - 1);
}console.log(factorial(5)); // 120
What You Learn:
- Recursion
- Functions
- Mathematical logic
10. Find the Longest Word in a String
Problem: Find the longest word in a given sentence.
function longestWord(sentence) {
const words = sentence.split(" ");
return words.reduce((longest, word) => word.length > longest.length ? word : longest, "");
}console.log(longestWord("CodebyHer helps beginners learn coding")); // "beginners"
What You Learn:
- String splitting
- Array iteration with
reduce() - Problem-solving
Tips for Solving JavaScript Challenges
- Understand the problem: Read it carefully before coding.
- Break it down: Divide the task into smaller steps.
- Start simple: Write a basic version first, then optimize.
- Test thoroughly: Try edge cases to ensure correctness.
- Learn from mistakes: Analyze errors and improve logic.
Conclusion
JavaScript coding challenges are excellent for improving programming skills and building confidence as a beginner. From string manipulation and array operations to loops, functions, and recursion, practicing challenges helps you apply concepts and solve real-world problems. By solving these challenges regularly, you can prepare for technical interviews, improve problem-solving skills, and become a proficient JavaScript developer.