![]() |
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: Table of Content Using If-else StatementsIn 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.
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 operatorIn 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.
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) |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |