Problem overview
The method isNegative of the class Solution receives a number n. It doesn't return anything: it prints "negative" to the console when n is less than zero, and "positive" otherwise. Its return type is void, which says that the method's job is the printing.
The three examples cover the cases. For n = 5 the method prints positive. For n = -2 it prints negative. The surprising one is n = 0: zero is not less than zero, so it also prints positive, even though zero is neither positive nor negative in mathematics. The task defines "positive" as "not negative".
Approach
This is a decision between two outcomes, which is exactly what an if-else statement does. One comparison is enough: ask whether n < 0. If the answer is yes, print one word. In every other case, print the other. There is no need for a second condition, because "otherwise" already covers zero and all positive numbers.
Writing a second test such as n > 0 would be a trap: it would leave zero without any output, or force a third branch. A single < with an else splits all numbers into two groups with no gap.
The branches could be written the other way round, with if (n >= 0) printing positive first. Both versions give the same output for every ordinary number. The lesson tests for the negative case first because that is the way the task is worded, and it keeps the condition a plain <.
Step by step
- Write the
ifwith its condition. Inside the method body, useif (n < 0) { }. The operator<is strict, so only values below zero get in. - Print in the
ifblock. Putconsole.log("negative");inside, so the word appears only when the condition holds. - Add the
else. Attachelse { }to the same statement and print"positive"there. It runs whenn < 0is false, which includes0.
The solution in TypeScript
class Solution {
isNegative(n: number): void {
if (n < 0) {
console.log("negative");
} else {
console.log("positive");
}
}
}
A few TypeScript details are worth noting:
- The annotations are for the compiler.
n: numberand: voidare checked while compiling and erased from the JavaScript that runs. A call such asisNegative("-3")gets a compiler error, but the JavaScript would still run, and the string would be converted to a number by<. numbercovers integers and decimals. There is just one numeric type, so-0.5works exactly like-2.console.logdoes the output. It prints the text and moves to a new line. The method returns nothing, so there is noreturnstatement.- It is a method on a class. To try it yourself you create an object first:
const s = new Solution();and then calls.isNegative(-2);.
Edge cases and common mistakes
- Zero.
0printspositive. Using<=would printnegativefor zero. - Decimals.
-0.5printsnegativeand0.5printspositive. - Negative zero. JavaScript has a value
-0, but-0 < 0is false, so it printspositive, like zero. NaN. Every comparison withNaNis false, soNaN < 0is false and it printspositive. The tests don't try this, but it shows why theelsebranch is "everything else", not "greater than zero".- Returning instead of printing. Writing
return "negative"would produce no output on the console. The task wants printing. - Wrong message text. The words are lowercase with no extra spaces or punctuation:
"negative"and"positive". - Using
=or a missing brace.if (n = 0)would assign. Unbalanced braces make the compiler report an error.
Testing
n = 5printspositive.n = 0printspositive.n = -2printsnegative.n = -0.5printsnegative.n = 1000000printspositive.