Start learning free

LESSON 129 OF 410 · LOOPS & CONDITIONALS

Print Even - Odd in JavaScript (Time Complexity: O(n))

Practice Read the full explanation

Given a positive integer n, for each number from 0 to n - 1, print "odd" if the number is odd or the number itself if it is even.

Example:

Input: n = 6
Output (console):
0
odd
2
odd
4
odd

Note: You have to print to the console, your function shouldn't return anything.

STEP 1 OF 3
    Ctrl + Enter to check

    JAVASCRIPT · LOOPS & CONDITIONALS

    Print Even Odd in JavaScript, explained

    Understanding the Problem

    The core challenge of this problem is to iterate through a range of numbers and determine if each number is odd or even. If the number is odd, we print "odd"; if it is even, we print the number itself. This problem is significant in learning basic control flow and conditional statements in programming. Common applications include filtering and processing sequences of numbers.

    Approach

    To solve this problem, we can use a simple loop to iterate through the numbers from 0 to n-1. For each number, we check if it is odd or even using the modulus operator (%). If the number is odd (i.e., number % 2 !== 0), we print "odd". Otherwise, we print the number itself.

    Naive Solution

    The naive solution involves iterating through each number and using an if-else statement to determine if the number is odd or even. This approach is straightforward and works well for this problem.

    Optimized Solution

    Given the simplicity of the problem, the naive solution is already optimal. The time complexity is O(n) because we need to check each number once, and the space complexity is O(1) since we are not using any additional data structures.

    Algorithm

    1. Start a loop from 0 to n-1.
    2. For each number, check if it is odd or even using the modulus operator (%).
    3. If the number is odd, print "odd".
    4. If the number is even, print the number itself.

    Code Implementation

    function printEvenOdd(n) {
      // Loop from 0 to n-1
      for (let i = 0; i < n; i++) {
        // Check if the number is odd
        if (i % 2 !== 0) {
          console.log("odd");
        } else {
          // If the number is even, print the number
          console.log(i);
        }
      }
    }
    
    // Example usage:
    printEvenOdd(6); // Output: 0, odd, 2, odd, 4, odd
    

    Complexity Analysis

    The time complexity of this solution is O(n) because we iterate through each number from 0 to n-1 exactly once. The space complexity is O(1) since we are not using any additional data structures.

    Edge Cases

    Potential edge cases include:

    • n = 1: The output should be "0".
    • n = 0: There should be no output since the range is empty.

    These edge cases are handled naturally by the loop and conditional statements.

    Testing

    To test the solution comprehensively, consider the following test cases:

    • n = 6: Output should be 0, odd, 2, odd, 4, odd.
    • n = 1: Output should be 0.
    • n = 0: No output.
    • n = 10: Output should be 0, odd, 2, odd, 4, odd, 6, odd, 8, odd.

    These test cases cover a range of scenarios from small to larger values of n.

    Thinking and Problem-Solving Tips

    When approaching such problems, it is helpful to:

    • Break down the problem into smaller, manageable parts.
    • Use simple control flow statements to handle different conditions.
    • Test your solution with a variety of inputs to ensure it handles all edge cases.

    Practicing similar problems and studying basic algorithms can help improve problem-solving skills.

    Conclusion

    In this blog post, we discussed how to solve the problem of printing "odd" for odd numbers and the number itself for even numbers in a given range. 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.

    Additional Resources

    For further reading and practice, consider the following 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