From 1752dfc32ad0dbea8e4a366fb176f560d5777107 Mon Sep 17 00:00:00 2001 From: Rubin Bhandari Date: Mon, 5 Oct 2020 01:24:03 +0545 Subject: [PATCH] added randomString --- snippets/randomString.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 snippets/randomString.md diff --git a/snippets/randomString.md b/snippets/randomString.md new file mode 100644 index 000000000..c716e0048 --- /dev/null +++ b/snippets/randomString.md @@ -0,0 +1,18 @@ +--- +title: randomString +tags: math,random,beginner +--- + +Returns a random string of the specified length. + +- `Math.random()` generate a random number +- `ToString(16)` converts the number into a text string +- Finally, the `substr(2,8)` extracts chracters between the positions 2 and supplied length. + +```js +const randomString = (length) => Math.random().toString(20).substr(2, length) +``` + +```js +randomString(0afad); // '0afad' +```