# Both Zero, Negative or Positive
Write a function that checks if two numbers are: Smaller than 0 Greater than 0 Exactly 0
# Examples
both(6, 2) ➞ true
both(0, 0) ➞ true
both(-1, 2) ➞ false
both(0, 2) ➞ false
1
2
3
4
5
6
7
2
3
4
5
6
7
# Notes
Inputs will always be two numbers.
# 答案(仅供参考)
# 解法 1
function both(n1, n2) {
return (n1 > 0 && n2 > 0) || (n1 < 0 && n2 < 0) || (n1 == 0 && n2 == 0);
}
1
2
3
2
3
# 解法 2
const both = (n1, n2) => Math.sign(n1) === Math.sign(n2);
1
# 解法 3
function both(n1, n2) {
return n1 * n2 > 0 || n1 === n2;
}
1
2
3
2
3