Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Sign of a Number

Fundamentals: Part 1easy

Course · Section 2: JavaScript Fundamentals – Part 1 · Lecture 13: Basic Operators

Implement `sign(n)` returning `-1`, `0`, or `1` for negative, zero, and positive numbers using a ternary expression.

Sample tests

Input: sign(5)
Output: 1
Input: sign(-2)
Output: -1

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Returning a boolean instead of -1, 0, or 1.

Learning resources

  • MDN: Expressions and operators
Visualize this concept: Operator Precedence and Associativity →
Approach & explanation (try first)

A nested ternary maps positive to 1, negative to -1, and zero to 0.

Loading...
⌘/Ctrl + Enter

Run your code to see results.