forked from anoniscoding/yorlang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add yipo helper function to return a random number anoniscoding#53
- Loading branch information
Showing
3 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Returns a random number between two values (0 -> 1 by default) | ||
* - If only one argument is specified, that will become the maximum range, with 0 as the minimum. | ||
* - If two arguments are specified, the first will be the minimum, and the second will be the maximum. | ||
* | ||
* @returns {number} | ||
*/ | ||
function yipo (args) { | ||
if (Array.isArray(args)) { | ||
let [ min, max, ] = args; | ||
min = Number(min) || 0; | ||
max = Number(max) || 0; | ||
|
||
if (min && !max) { | ||
max = min; | ||
min = 0; | ||
} | ||
|
||
if (!max) max = 1; | ||
|
||
return min + (Math.random() * (max - min)); | ||
} | ||
throw new Error("Yorlang system error"); | ||
} | ||
|
||
module.exports = yipo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const yipo = require("../../../helperise/random_helpers/yipo.js"); | ||
|
||
describe("Yipo Test suite", () => { | ||
test("It should return a random number between 0 and 1", () => { | ||
for (let i = 1; i <= 5; i++) { | ||
expect(yipo([])).toBeGreaterThan(0) | ||
expect(yipo([])).toBeLessThan(1) | ||
} | ||
}); | ||
|
||
test("It should return a random number between 1 and 2", () => { | ||
for (let i = 1; i <= 5; i++) { | ||
expect(yipo([1, 2])).toBeGreaterThan(1) | ||
expect(yipo([1, 2])).toBeLessThan(2) | ||
} | ||
}); | ||
|
||
test("It should return a random number between 0 and 7", () => { | ||
for (let i = 1; i <= 5; i++) { | ||
expect(yipo([7])).toBeGreaterThan(0) | ||
expect(yipo([7])).toBeLessThan(7) | ||
} | ||
}); | ||
}); |
7744975
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beautiful