https://www.codewars.com/kata/55685cd7ad70877c23000102/javascript
Codewars - Achieve mastery through coding practice and developer mentorship
Coding practice for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!
www.codewars.com
DESCRIPTION:
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.
설명:
숫자를 받고 그 숫자를 음수로 반환하는 간단한 과제이다. 하지만 숫자가 이미 음수라면?
예시
makeNegative(1); // return -1
makeNegative(-5); // return -5
makeNegative(0); // return 0
makeNegative(0.12); // return -0.12
주의
- 숫자는 이미 음수일수 있고, 그럴경우 변경할 필요가 없습니다.
- 영(0) 특정한 부호를 가지고 있지 않습니다. 음수 영은 수학적으로 의미가 없습니다.
풀이
function makeNegative(num) {
if(num <= 0) return num;
else return num * -1;
}
num이 0과 같거나 작을 때는 num 그대로를 반환하고 num이 0보다 클 경우 num에 -1을 곱한 값을 반환했다(-num로 반환해도 된다).
'알고리즘 > Codewars' 카테고리의 다른 글
[Codewars] Are they the "same"? (6 kyu) / JavaScript (0) | 2022.10.13 |
---|---|
[Codewars] Number of People in the Bus (7 kyu) / JavaScript (0) | 2022.10.13 |
[Codewars] Convert a Boolean to a String (8 kyu) / JavaScript (0) | 2022.10.12 |
[Codewars] Sum Arrays (8 kyu) / JavaScript (0) | 2022.10.12 |
[Codewars] Find the odd int (6 kyu) / JavaScript (0) | 2022.10.12 |
댓글