558 B
558 B
title, tags, expertise, author, cover, firstSeen, lastUpdated
| title | tags | expertise | author | cover | firstSeen | lastUpdated |
|---|---|---|---|---|---|---|
| Copy sign to number | math | beginner | maciv | blog_images/keyboard-tea.jpg | 2020-10-07T23:52:57+03:00 | 2020-10-07T23:52:57+03:00 |
Returns the absolute value of the first number, but the sign of the second.
- Use
Math.sign()to check if the two numbers have the same sign. - Return
xif they do,-xotherwise.
const copySign = (x, y) => Math.sign(x) === Math.sign(y) ? x : -x;
copySign(2, 3); // 2
copySign(2, -3); // -2
copySign(-2, 3); // 2
copySign(-2, -3); // -2