Start learning free

LESSON 75 OF 410 · CONDITIONAL STATEMENTS

Is Number Negative? (JavaScript, O(1) Time Complexity)

Practice Read the full explanation

Create a function that takes a number n as its only argument and prints "negative" to the console if n is less than zero, otherwise prints "positive" to the console.

Example 1:

Input: n = 5
Output: "positive"
Explanation:
5 is greater than or equal to zero, so our function prints "positive" to the console.

Example 2:

Input: n = 0
Output: "positive"
Explanation:
0 is greater than or equal to zero, so our function prints "positive" to the console.

Example 3:

Input: n = -2
Output: "negative"
Explanation:
-2 is less than zero, so our function prints "negative" to the console.

Hints:

You can make use of < operator with an if statement to check if n is negative.

You can add an else statement to handle the case when n is positive.
STEP 1 OF 3
    Ctrl + Enter to check

    JAVASCRIPT · CONDITIONAL STATEMENTS

    Is Number Negative in JavaScript, explained

    Understanding the Problem

    The core challenge of this problem is to determine whether a given number is negative or not. This is a fundamental problem in programming that helps in understanding conditional statements and basic comparison operations.

    Common applications of this problem include financial calculations, temperature readings, and any scenario where distinguishing between positive and negative values is crucial.

    Potential pitfalls include misunderstanding the comparison operators or not handling the zero case correctly.

    Approach

    To solve this problem, we can use a simple conditional statement to check if the number is less than zero. If it is, we print "negative"; otherwise, we print "positive". This approach is straightforward and efficient.

    Let's break down the approach:

    • Check if the number is less than zero using the < operator.
    • If the condition is true, print "negative".
    • If the condition is false, print "positive".

    Algorithm

    Here is a step-by-step breakdown of the algorithm:

    1. Take the input number n.
    2. Use an if statement to check if n < 0.
    3. If the condition is true, print "negative".
    4. If the condition is false, print "positive".

    Code Implementation

    function checkNumber(n) {
      // Check if the number is less than zero
      if (n < 0) {
        console.log("negative");
      } else {
        console.log("positive");
      }
    }
    
    // Test cases
    checkNumber(5);  // Output: "positive"
    checkNumber(0);  // Output: "positive"
    checkNumber(-2); // Output: "negative"
    

    Complexity Analysis

    The time complexity of this solution is O(1) because the comparison and print operations take constant time. The space complexity is also O(1) as we are not using any additional data structures.

    Edge Cases

    Potential edge cases include:

    • Zero: The function should correctly identify zero as "positive".
    • Large positive and negative numbers: The function should handle large values without any issues.

    Examples of edge cases and their expected outputs:

    checkNumber(0);    // Output: "positive"
    checkNumber(-1000000); // Output: "negative"
    checkNumber(1000000);  // Output: "positive"
    

    Testing

    To test the solution comprehensively, we can use a variety of test cases:

    • Simple positive number: checkNumber(5);
    • Zero: checkNumber(0);
    • Simple negative number: checkNumber(-2);
    • Large positive number: checkNumber(1000000);
    • Large negative number: checkNumber(-1000000);

    We can use JavaScript's built-in console for testing, or frameworks like Jest for more comprehensive testing.

    Thinking and Problem-Solving Tips

    When approaching such problems, it's essential to:

    • Understand the problem requirements and constraints.
    • Break down the problem into smaller, manageable steps.
    • Consider edge cases and how to handle them.
    • Write clean, readable code with comments explaining the logic.

    To improve problem-solving skills, practice solving similar problems, study algorithms, and participate in coding challenges.

    Conclusion

    In this blog post, we discussed how to determine if a number is negative using a simple conditional statement in JavaScript. 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.

    We encourage readers to practice and explore further to enhance their 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