Skip to content

Commit b711214

Browse files
committed
feat: randomSign()
1 parent ca1724a commit b711214

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ https://unpkg.com/math-toolbox/dist/math-toolbox.umd.min.js
7676
| isEven | Returns true if the number given is even. |
7777
| difference | Returns the absolute difference between two values. |
7878
| within | Checks if two values are within the given tolerance of each other |
79+
| randomSign | Returns either 1 or -1 |
7980

8081
## Plans
8182
See GitHub Wiki:

docs/index.html

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,16 @@ <h3 class='mb0 no-anchor'></h3>
303303

304304
</li>
305305

306+
307+
<li><a
308+
href='#randomsign'
309+
class="">
310+
randomSign
311+
312+
</a>
313+
314+
</li>
315+
306316
</ul>
307317
</div>
308318
<div class='mt1 h6 quiet'>
@@ -2462,6 +2472,58 @@ <h3 class='fl m0' id='within'>
24622472

24632473

24642474

2475+
</section>
2476+
2477+
2478+
2479+
2480+
<section class='p2 mb2 clearfix bg-white minishadow'>
2481+
2482+
2483+
<div class='clearfix'>
2484+
<h3 class='fl m0' id='randomsign'>
2485+
randomSign
2486+
</h3>
2487+
2488+
</div>
2489+
2490+
2491+
<p>Generate random sign (1 or -1)</p>
2492+
2493+
2494+
<div class='pre p1 fill-light mt0'>randomSign(): <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a></div>
2495+
2496+
2497+
2498+
2499+
2500+
2501+
2502+
2503+
2504+
2505+
2506+
2507+
2508+
2509+
2510+
<div class='py1 quiet mt1 prose-big'>Returns</div>
2511+
<code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a></code>:
2512+
Either 1 or -1
2513+
2514+
2515+
2516+
2517+
2518+
2519+
2520+
2521+
2522+
2523+
2524+
2525+
2526+
24652527
</section>
24662528

24672529

src/math-toolbox.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ export { isEven } from './is-even'
2626
export { percent } from './percent'
2727
export { difference } from './difference'
2828
export { within } from './within'
29+
export { randomSign } from './random-sign'

src/random-sign.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Generate random sign (1 or -1)
3+
*
4+
* @return {number} Either 1 or -1
5+
*/
6+
function randomSign () {
7+
return (Math.random() > 0.5) ? 1 : -1
8+
}
9+
10+
export { randomSign }

test/random-sign.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { randomSign } from '../src/math-toolbox'
2+
3+
describe('Return random sign 1 or -1', () => {
4+
let sign = randomSign()
5+
6+
it('Should be greater than or equal to -1', () => {
7+
expect(sign).toBeGreaterThanOrEqual(-1)
8+
})
9+
10+
it('Should be less than or equal to 1', () => {
11+
expect(sign).toBeLessThanOrEqual(1)
12+
})
13+
})

0 commit comments

Comments
 (0)