Whether you're preparing for a coding interview or looking to sharpen your problem-solving skills, practicing algorithms is key to mastering JavaScript. Algorithms are not just for advanced developers—they’re fundamental for understanding how to manipulate data and optimize performance in your applications.
In this post, we'll explore some essential JavaScript algorithm questions that every developer should practice. These questions cover a range of topics, from basic array manipulation to more complex string handling, giving you a solid foundation for tackling more advanced problems.
1. Reverse a String
Problem:
Write a function that takes a string as input and returns the string reversed.
Example:
// Input: "hello"
// Output: "olleh"
Solution:
There are multiple ways to reverse a string in JavaScript. One of the simplest methods is to split the string into an array of characters, reverse the array, and then join it back into a string.
function reverseString(str) {
return str.split('').reverse().join('');
}
console.log(reverseString("hello")); // Output: "olleh"
2. Check for Palindromes
Problem:
A palindrome is a word or phrase that reads the same backward as forward. Write a function to check if a given string is a palindrome.
Example:
// Input: "racecar"
// Output: true
// Input: "hello"
// Output: false
Solution:
You can reuse the string reversal method to check if the original string is equal to its reverse.
function isPalindrome(str) {
const reversedStr = str.split('').reverse().join('');
return str === reversedStr;
}
console.log(isPalindrome("racecar")); // Output: true
console.log(isPalindrome("hello")); // Output: false
3. Find the Largest Number in an Array
Problem:
Write a function that takes an array of numbers as input and returns the largest number.
Example:
// Input: [1, 2, 3, 4, 5]
// Output: 5
Solution:
You can use the Math.max
method in combination with the spread operator to find the largest number.
function findLargest(arr) {
return Math.max(...arr);
}
console.log(findLargest([1, 2, 3, 4, 5])); // Output: 5
4. FizzBuzz
Problem:
Write a function that prints numbers from 1 to 100. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five, print "Buzz". For numbers which are multiples of both three and five, print "FizzBuzz".
Solution:
This is a classic interview question that tests your ability to implement basic loops and conditionals.
function fizzBuzz() {
for (let i = 1; i <= 100; 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);
}
}
}
fizzBuzz();
5. Factorialize a Number
Problem:
Write a function that returns the factorial of a number. The factorial of a number n
is the product of all positive integers less than or equal to n
.
Example:
// Input: 5
// Output: 120 (5 * 4 * 3 * 2 * 1)
Solution:
Factorials can be solved recursively or iteratively. Here’s an example using recursion:
function factorialize(num) {
if (num === 0 || num === 1) {
return 1;
} else {
return num * factorialize(num - 1);
}
}
console.log(factorialize(5)); // Output: 120
6. Find the Longest Word in a String
Problem:
Write a function that takes a string as input and returns the length of the longest word.
Example:
// Input: "The quick brown fox jumped over the lazy dog"
// Output: 6 (jumped)
Solution:
You can split the string into an array of words and then reduce the array to find the longest word.
function findLongestWord(str) {
const words = str.split(' ');
let maxLength = 0;
for (let word of words) {
if (word.length > maxLength) {
maxLength = word.length;
}
}
return maxLength;
}
console.log(findLongestWord("The quick brown fox jumped over the lazy dog")); // Output: 6
7. Remove Duplicates from an Array
Problem:
Write a function that removes duplicate values from an array.
Example:
// Input: [1, 2, 2, 3, 4, 4, 5]
// Output: [1, 2, 3, 4, 5]
Solution:
One of the simplest ways to remove duplicates is to use a Set
, which only stores unique values.
function removeDuplicates(arr) {
return [...new Set(arr)];
}
console.log(removeDuplicates([1, 2, 2, 3, 4, 4, 5])); // Output: [1, 2, 3, 4, 5]
Conclusion
Practicing these fundamental JavaScript algorithm questions will enhance your problem-solving skills and prepare you for more advanced challenges. By mastering these basics, you’ll be better equipped to tackle complex algorithms and data structures, which are essential for writing efficient, scalable code. Keep practicing, and you’ll see improvements in both your coding speed and accuracy!
Top comments (0)