Start learning free

LESSON 128 OF 410 · LOOPS & CONDITIONALS

Even Numbers From Array: Buggy Code in JavaScript (Time Complexity: O(n))

Practice Read the full explanation

Inside the code editor we've tried to write a function that takes an array nums as argument and prints to the console the even numbers from that array.

So when we called printEvenNumbers([2, 1, 0, 4, 3]), we expected our code to print:

2
0
4

but it seems like we made some mistakes because when we run our code, it prints:

even
even

Assignment:

Your task is to fix our function such that it prints the even numbers from the array.

STEP 1 OF 1
    Ctrl + Enter to check

    JAVASCRIPT · LOOPS & CONDITIONALS

    Buggy Code Print Even Numbers in JavaScript, explained

    Understanding the Problem

    The core challenge here is to correctly identify and print even numbers from the given array. Even numbers are integers that are divisible by 2 without a remainder. This problem is common in scenarios where filtering specific data from a dataset is required.

    Potential pitfalls include misunderstanding the condition for even numbers or incorrectly iterating through the array.

    Approach

    To solve this problem, we need to iterate through the array and check each number to see if it is even. If it is, we print it. A naive solution might involve incorrect conditions or improper iteration, leading to incorrect outputs.

    Let's start with a simple approach:

    function printEvenNumbers(nums) {
      for (let i = 0; i < nums.length; i++) {
        if (nums[i] % 2 === 0) {
          console.log(nums[i]);
        }
      }
    }
    

    This approach iterates through the array once, checking each number. If the number is even, it prints the number.

    Algorithm

    1. Start a loop from the beginning of the array to the end.

    2. For each element, check if it is divisible by 2 (i.e., even).

    3. If it is even, print the number.

    Code Implementation

    function printEvenNumbers(nums) {
      // Loop through each number in the array
      for (let i = 0; i < nums.length; i++) {
        // Check if the number is even
        if (nums[i] % 2 === 0) {
          // Print the even number
          console.log(nums[i]);
        }
      }
    }
    
    // Test the function with the given example
    printEvenNumbers([2, 1, 0, 4, 3]); // Expected output: 2, 0, 4
    

    Complexity Analysis

    The time complexity of this approach is O(n), where n is the number of elements in the array. This is because we are iterating through the array once. The space complexity is O(1) as we are not using any extra space that scales with the input size.

    Edge Cases

    Consider the following edge cases:

    • An empty array: The function should not print anything.
    • An array with no even numbers: The function should not print anything.
    • An array with all even numbers: The function should print all numbers.

    Examples:

    printEvenNumbers([]); // Expected output: (nothing)
    printEvenNumbers([1, 3, 5]); // Expected output: (nothing)
    printEvenNumbers([2, 4, 6]); // Expected output: 2, 4, 6
    

    Testing

    To test the solution comprehensively, we should include a variety of test cases:

    • Simple cases with a mix of even and odd numbers.
    • Edge cases as discussed above.
    • Large arrays to ensure performance.

    Example test cases:

    printEvenNumbers([2, 1, 0, 4, 3]); // Expected output: 2, 0, 4
    printEvenNumbers([7, 8, 10, 13, 15]); // Expected output: 8, 10
    printEvenNumbers([1, 3, 5, 7]); // Expected output: (nothing)
    printEvenNumbers([2, 4, 6, 8]); // Expected output: 2, 4, 6, 8
    

    Thinking and Problem-Solving Tips

    When approaching such problems, consider the following tips:

    • Understand the problem requirements and constraints thoroughly.
    • Start with a simple solution and then optimize.
    • Think about edge cases and how your solution handles them.
    • Write clean, readable code with comments to explain your logic.

    To improve problem-solving skills, practice regularly on coding challenge platforms and study different algorithms and data structures.

    Conclusion

    In this blog post, we discussed how to fix a function to print even numbers from an array. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for developing strong programming skills.

    Keep practicing and exploring further to enhance your problem-solving abilities.

    Additional Resources

    Keep going with JavaScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free