반응형
In this simple assignment you are given a number and have to make it negative. But maybe the number is already negative?
Examples
makeNegative(1); // return -1
makeNegative(-5); // return -5
makeNegative(0); // return 0
makeNegative(0.12); // return -0.12
Notes
- The number can be negative already, in which case no change is required.
- Zero (0) is not checked for any specific sign. Negative zeros make no mathematical sense.
Solution:
num makeNegative(n) {
if (n == 0) {
return 0;
} else if (n > 0) {
return n * -1;
} else {
return n;
}
}
num makeNegative(n) => -n.abs();
num makeNegative(n) => n > 0 ? -n : n;
num makeNegative(n)=> !n.isNegative ? -n : n;
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
1121. Grasshopper - Grade book (0) | 2022.11.22 |
---|---|
1120. Determine offspring sex based on genes XX and XY chromosomes (0) | 2022.11.20 |
1118. Function 1 - hello world (0) | 2022.11.18 |
1117. Reverse List Order (0) | 2022.11.17 |
1116. Convert boolean values to strings 'Yes' or 'No'. (0) | 2022.11.17 |