🚀 Boost Your JavaScript Skills with More Coding Challenges! 🚀
JavaScript learners and enthusiasts, it’s time to sharpen those coding skills with five new exercises:
1. FizzBuzz: Print numbers from 1 to n
, replacing multiples of 3 with “Fizz,” multiples of 5 with “Buzz,” and multiples of both with “FizzBuzz.”
2. Find the Second Largest Element: Discover the second largest element in an array of numbers.
3. Calculate the Fibonacci Sequence: Generate the Fibonacci sequence up to n
terms.
4. Capitalize Words in a Sentence: Capitalize the first letter of each word in a sentence.
5. Remove Duplicates from an Array: Eliminate duplicates from an array using a simple technique.
These exercises offer diverse challenges to improve your problem-solving and JavaScript skills. Whether you’re a beginner or seasoned coder, they provide valuable practice for tackling real-world scenarios. Happy coding! 💻🔥 #JavaScript #CodingExercises #Programming #ProblemSolving #TechSkills #JavaScriptLearners
Exercise 6: FizzBuzz
Description: Write a JavaScript function that prints numbers from 1 to n, but for multiples of 3, print “Fizz” instead of the number, and for multiples of 5, print “Buzz.” For numbers that are multiples of both 3 and 5, print “FizzBuzz.”
function fizzBuzz(n) {
for (let i = 1; i <= n; 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);
}
}
}
// Example usage:
fizzBuzz(15); // Prints FizzBuzz, 1, 2, Fizz, 4, Buzz, …
Summary: This function uses a for loop to iterate from 1 to n and checks each number for divisibility by 3 and 5 to print “Fizz,” “Buzz,” or “FizzBuzz” accordingly.
Exercise 7: Find the Second Largest Element
Description: Write a JavaScript function to find the second largest element in an array of numbers.
function findSecondLargestElement(arr) {
arr.sort((a, b) => b – a);
return arr[1];
}
// Example usage:
const numbers = [10, 5, 8, 20, 3];
const secondLargest = findSecondLargestElement(numbers); // Returns 10
Summary: This function sorts the array in descending order and returns the second element, which is the second largest element in the array.
Exercise 8: Calculate the Fibonacci Sequence
Description: Write a JavaScript function to generate the Fibonacci sequence up to n terms.
function generateFibonacci(n) {
const fibonacci = [0, 1];
for (let i = 2; i < n; i++) {
fibonacci.push(fibonacci[i – 1] + fibonacci[i – 2]);
}
return fibonacci;
}
// Example usage:
const fibonacciSequence = generateFibonacci(10); // Returns [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Summary: This function uses a for loop to generate the Fibonacci sequence up to n terms and stores the results in an array.
Exercise 9: Capitalize Words in a Sentence
Description: Write a JavaScript function that capitalizes the first letter of each word in a sentence.
function capitalizeWords(sentence) {
return sentence
.split(” “)
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(” “);
}
// Example usage:
const originalSentence = “hello world”;
const capitalizedSentence = capitalizeWords(originalSentence); // Returns “Hello World”
Summary: This function splits the sentence into words, capitalizes the first letter of each word, and then joins them back into a sentence.
Exercise 10: Remove Duplicates from an Array
Description: Write a JavaScript function to remove duplicates from an array.
function removeDuplicates(arr) {
return Array.from(new Set(arr));
}
// Example usage:
const numbers = [1, 2, 2, 3, 4, 4, 5];
const uniqueNumbers = removeDuplicates(numbers); // Returns [1, 2, 3, 4, 5]
Summary: This function converts the array to a Set (which automatically removes duplicates) and then converts it back to an array.
These JavaScript coding exercises cover a range of common programming tasks and can help you practice your problem-solving and coding skills.