Open In App

JavaScript Program to Return Quadrants in which a given Coordinate Lies

Last Updated : 22 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

There are four quadrants in cartesian coordinate system. The names of the quadrants are: Quadrant I, Quadrant II, Quadrant III, and Quadrant IV.

The below approaches can be used to find the quadrant of the given coordinate:

Using If-else Statements

In this approach, we will check the value of both X and Y coordinates using the if-else statement in JavaScript. The conditional statement will check whether the value is positive or negative and return their quadrant based on them.

Example: The below code will explain the use of the if-else statement to check the quadrant of the given coordinate.

JavaScript
function getQuadrants(x, y) {
    if (x > 0 && y > 0) {
        return `The point (${x}, ${y}) lies in Quadrant I`;
    } else if (x < 0 && y > 0) {
        return `The point (${x}, ${y}) lies in Quadrant II`;
    } else if (x < 0 && y < 0) {
        return `The point (${x}, ${y}) lies in Quadrant III`;
    } else if (x > 0 && y < 0) {
        return `The point (${x}, ${y}) lies in Quadrant IV`;
    } else if (x === 0 && y !== 0) {
        return `The point (${x}, ${y}) lies on the y-axis`;
    } else if (x !== 0 && y === 0) {
        return `The point (${x}, ${y}) lies on the x-axis`;
    } else {
        return `The point (${x}, ${y}) lies at the origin (0, 0)`;;
    }
}
console.log(getQuadrants(5, 8));
console.log(getQuadrants(-2, 7));
console.log(getQuadrants(-7, -9));
console.log(getQuadrants(3, -6));

Output
The point (5, 8) lies in Quadrant I
The point (-2, 7) lies in Quadrant II
The point (-7, -9) lies in Quadrant III
The point (3, -6) lies in Quadrant IV

Time Complexity : O(1)

Space Complexity : O(1)

Using the ternary operator

In this approach, we will use ternary operator to determine in which quadrant coordinate point will lie. We will define a condition and its corresponding quadrant if condition comes true. Otherwise, We will again define a condition for next quadrant and repeat the same condition chain for all possible cases.

Example: The below code will explain the use of JavaScript ternary operator to check quadrant of the given coordinates.

JavaScript
function getQuadrant(x, y) {
    return (x > 0 && y > 0) ? "Quadrant I" :
           (x < 0 && y > 0) ? "Quadrant II" :
           (x < 0 && y < 0) ? "Quadrant III" :
           (x > 0 && y < 0) ? "Quadrant IV" :
           (x === 0 && y !== 0) ? "lies on y-axis" :
           (x !== 0 && y === 0) ? "lies on x-axis" :
           "lies on Origin";
}
console.log(getQuadrant( 2 , 3));   
console.log(getQuadrant(-2 , 3));  
console.log(getQuadrant(-2, -3)); 
console.log(getQuadrant(2, -3));  
console.log(getQuadrant(0, 2));   
console.log(getQuadrant(2, 0));   
console.log(getQuadrant(0, 0));   

Output
Quadrant I
Quadrant II
Quadrant III
Quadrant IV
lies on y-axis
lies on x-axis
lies on Origin

Time Complexity : O(1)

Space Complexity : O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads