MC, 2025
Ilustracja do artykułu: Boost Your JavaScript Skills: Fun Exercises with Answers!

Boost Your JavaScript Skills: Fun Exercises with Answers!

Are you ready to level up your JavaScript skills? Whether you're a beginner or looking to sharpen your expertise, hands-on practice is essential to mastering JavaScript. In this article, we'll explore some fun and engaging JavaScript exercises with answers, designed to help you improve your coding abilities. Let’s dive in and start solving these challenges!

Why Practice with JavaScript Exercises?

Practicing with JavaScript exercises is one of the most effective ways to become proficient in coding. JavaScript is a versatile language used for both front-end and back-end development, and it’s essential to have a strong grasp of its core concepts. But how do you get better at it? The answer is simple: practice!

When you tackle exercises with different levels of difficulty, you not only reinforce the syntax but also improve your problem-solving skills. Plus, by solving challenges, you gain confidence in applying JavaScript to real-world situations. So, let’s get started with some exciting exercises!

Exercise 1: Simple Number Check

Let’s begin with a simple exercise to get you comfortable with JavaScript syntax. Your task is to write a function that checks if a given number is positive, negative, or zero. Here’s the exercise description:

Task: Write a function that takes a number as an argument and returns “positive”, “negative”, or “zero” depending on the value of the number.

Solution: Below is the JavaScript code for this exercise:

function checkNumber(num) {
  if (num > 0) {
    return "positive";
  } else if (num < 0) {
    return "negative";
  } else {
    return "zero";
  }
}

console.log(checkNumber(5)); // positive
console.log(checkNumber(-3)); // negative
console.log(checkNumber(0)); // zero

This simple function demonstrates the use of conditional statements (if-else) in JavaScript. You can try it with different numbers to test how the function behaves.

Exercise 2: FizzBuzz Challenge

The famous FizzBuzz challenge is a great exercise for practicing loops and conditionals in JavaScript. Here's the challenge:

Task: Write a function that prints numbers from 1 to 100. For multiples of 3, print “Fizz”, for multiples of 5, print “Buzz”, and for numbers that are multiples of both 3 and 5, print “FizzBuzz”.

Solution: Here's the JavaScript code to solve this challenge:

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();

This challenge is an excellent way to practice working with loops and the modulo operator in JavaScript. Try modifying the code to print different messages or solve a similar problem with your own twist!

Exercise 3: Palindrome Checker

Next, let’s tackle a string manipulation exercise. This challenge will help you work with string methods and conditional statements. Here’s the exercise:

Task: Write a function that checks if a given string is a palindrome (i.e., it reads the same backward as forward). The function should ignore spaces and capitalization.

Solution: Here's how you can solve this exercise:

function isPalindrome(str) {
  const cleanedStr = str.replace(/\s+/g, '').toLowerCase();
  const reversedStr = cleanedStr.split('').reverse().join('');
  return cleanedStr === reversedStr;
}

console.log(isPalindrome("racecar")); // true
console.log(isPalindrome("hello")); // false
console.log(isPalindrome("A man a plan a canal Panama")); // true

In this solution, we first remove any spaces and convert the string to lowercase to handle case sensitivity. Then, we reverse the string and compare it to the original string. If they match, the string is a palindrome!

Exercise 4: Sum of All Numbers in an Array

Arrays are a fundamental concept in JavaScript, and working with them is essential. This exercise will help you practice iterating over an array to calculate the sum of its elements:

Task: Write a function that calculates the sum of all numbers in an array.

Solution: Here’s a simple solution using a loop:

function sumArray(arr) {
  let sum = 0;
  for (let i = 0; i < arr.length; i++) {
    sum += arr[i];
  }
  return sum;
}

console.log(sumArray([1, 2, 3, 4, 5])); // 15
console.log(sumArray([10, -5, 3, 8])); // 16

This exercise reinforces working with arrays and loops. You can experiment by passing arrays with different types of elements (numbers, strings, etc.) to see how the function behaves.

Exercise 5: Find the Largest Number in an Array

Now let’s take it up a notch with a more challenging array exercise. The task is to find the largest number in an array. This will help you practice working with arrays and comparison operators:

Task: Write a function that returns the largest number in an array of numbers.

Solution: Here’s how to solve this exercise:

function findLargestNumber(arr) {
  return Math.max(...arr);
}

console.log(findLargestNumber([1, 3, 5, 2, 4])); // 5
console.log(findLargestNumber([10, 20, 30, 5])); // 30

In this solution, we use the Math.max() function along with the spread operator to find the largest number in the array. This is a concise and efficient way to solve the problem.

Exercise 6: Reverse an Array

Reversing an array is a common task, and it’s a great way to practice array manipulation. Here’s the challenge:

Task: Write a function that reverses the order of elements in an array.

Solution: You can solve this easily with the reverse() method in JavaScript:

function reverseArray(arr) {
  return arr.reverse();
}

console.log(reverseArray([1, 2, 3, 4])); // [4, 3, 2, 1]
console.log(reverseArray([5, 10, 15])); // [15, 10, 5]

This exercise is straightforward but great for practicing basic array methods in JavaScript.

Conclusion: Keep Practicing!

JavaScript is an incredibly powerful and flexible programming language, and the more you practice, the better you’ll get. By tackling exercises like the ones listed above, you’ll gain confidence and become more proficient in your coding skills. Remember, there’s always something new to learn, and every problem is an opportunity to improve!

So, keep practicing, explore new challenges, and don't be afraid to make mistakes along the way. JavaScript is a fun language to work with, and with the right exercises, you’ll be a JavaScript pro in no time!

Komentarze (0) - Nikt jeszcze nie komentował - bądź pierwszy!

Imię:
Treść: