A small example on how to create a simple random string using a function.
You will need at least some knowledge of JavaScript if you want to use this example.
Example
Step by step guide
You will need at least some knowledge of JavaScript if you want to use this example.
Example
Code:
function randomNumber() {
var rand = ["1", "2", "3", "4", "5"]; //Place your strings in an array.
return rand[Math.floor(Math.random() * rand.length)]; //Randomizes your array.
}
console.log(randomNumber()); //Outputs your function.
Step by step guide
- First create a function and name it whatever you want. example: randomNumber
Code:
function randomNumber () {
}
- Create an array and set the variable to whatever you want. example: rand
Code:
var rand = ["1", "2", "3", "4", "5"];
- Then using the Math functions, randomize your array and return it.
Code:
return rand[Math.floor(Math.random() * rand.length)];
- Now just console.log() your function and run the file using node.